Sign in to follow this  
Followers 0
ChrisL

Why can't I close a file handle on an empty folder?

10 posts in this topic

I can't understand this, but if I do a file copy recursive and I encounter an empty folder I'm left with open filehandles..

does anyone know why this is please..

Thanks

#include <file.au3>
#include <Array.au3>
#include <GUIConstants.au3>



 $fileHandle = fileFindFirstFile("D:\Upload\*.lac")
    if ($fileHandle <> -1) then
            

        endif   
    
        
            if (@error = 1) then 
                Else
            $file = fileFindNextFile($fileHandle)
            ENDIF
            
            if ($file = "." or $file = "..") then 
                Else
            $Folder = StringtrimRight ($File, 4)
            Endif
            FileClose($FileHandle)

StartCopy ("d:\Upload\" & $Folder & "\", "\\bay1server\upload\"& $Folder)

        
    ;MsgBox (0,"","d:\Upload\" & $Folder & "\" & "\\bay1server\upload")
        
        
        
FileCopy ("d:\Upload\" & $File, "\\bay1server\upload\" & $File )
DirMove ("d:\Upload\" & $Folder, "d:\Upload\done\", 1)
FileMove ("d:\Upload\" & $File, "d:\Upload\done\", 1)

        



Func StartCopy ($Var1, $Var2)

 If StringRight($Var1, 1) = '\' Then $Var1 = StringtrimRight( $Var1, 1 )
 If StringRight($Var2, 1) <> '\' Then $Var2 = $Var2 & '\'

$Var3 = $Var1

FileSetAttrib ( $Var2 , "-R" , 1 )


processFolder($Var1, $Var2, $Var3)

        FileSetAttrib ( $Var2, "-R" , 1 )   
    
;Sleep (1000)


EndFunc


func processFolder($path,  $Destination, $Fixed)

    local $fileHandle, $file, $dirHandle, $dir
    
    

    local const $FILESPEC = "*.*"
 debug("Starting with " & $path)



 debug("Going to search for " & $path & "\" & $FILESPEC)
    $fileHandle = fileFindFirstFile($path & "\" & $FILESPEC)
    if ($fileHandle <> -1) then 
            
            
    
    
    
        while (1)
            $file = fileFindNextFile($fileHandle)
            if (@error = 1) then exitLoop
            if ($file = "." or $file = "..") then continueLoop
            $file = $path & "\" & $file
            if (isFolder($file,$Destination, $Fixed)) then continueLoop
            copy($file, $Destination, $Fixed)
            


        wEnd
endif       
        
        fileClose($fileHandle)

  

 debug("Going to search for subfolders")
    $dirHandle = fileFindFirstFile($path & "\*.*")
    if ($dirHandle <> -1) then
        
        while (1)
            $dir = fileFindNextFile($dirHandle)
            if (@error = 1) then exitLoop
            if ($dir = "." or $dir = "..") then continueLoop
            $dir = $path & "\" & $dir
            if (not(isFolder($dir,$Destination, $Fixed))) then continueLoop
      debug("Found subfolder " & $dir)
            processFolder($dir, $Destination, $Fixed)
        wEnd
        
    endIf
fileClose($dirHandle)
debug ($DirHandle)


endFunc

func isFolder($path,$Destination, $Fixed)

    if stringInStr(fileGetAttrib($path), "D") then 
    $stripped =stringreplace($Path, $Fixed &"\","")
    DirCreate ( $Destination & $Stripped )
    debug ("Path " & $Path)
    debug ("Creat Dir " & $Destination & $Stripped)
    return 1
    else
    return 0
    
    Endif
    
    
endFunc

func copy($text,$Destination, $Fixed)
    


$NewDir = $Destination
    $stripped =stringreplace($text, $fixed &"\","")
        
        FileCopy ($text, $NewDir & $Stripped,1)
        
        
endFunc




func debug($text)

MsgBox (0,"Debug", $Text)
    
   
endFunc

Share this post


Link to post
Share on other sites



Someone please help me.

It seems to be when it goes after a new directory that is empty, because it is exiting out of the isfolder function and then the variable is trashed, but the filehandle is still open. How can I get round this?

Thanks

Share this post


Link to post
Share on other sites

Someone please help me.

It seems to be when it goes after a new directory that is empty, because it is exiting out of the isfolder function and then the variable is trashed, but the filehandle is still open. How can I get round this?

Thanks

Could you be more explicit about what the problem is you are having ?

Which variable gets trashed ?

Why do you say that a FileHandle isn't closed ?


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Could you be more explicit about what the problem is you are having ?

Which variable gets trashed ?

Why do you say that a FileHandle isn't closed ?

It isn't able to move the empty folders after it has copied all of the files.

It should look for a file called 1234.lac (for example) then search the folder with the same name, copy all of the files and folders to another location on another computer (shared folder) then move all of the copied files to a done folder locally. But it can not move the empty folders as there is still a file lock on them. As soon as the program exits then the lock is released.

I think that it is Dirhandle thats the problem, when it changes dir to search for files and doesn't find any files in the empty folder it exits the isfolder loop and leaves the filehandle open on the empty dir. (I THINK!)

At least I think thats whats happening

I added some more lines into the program and made it output to a file.

I have attached them if anyone can work out where I have gone wrong.

Thanks

Share this post


Link to post
Share on other sites

Think you hit a BUG here.

I am not that familiar with this piece of code but it looks like a Handle isn't freed when there is no file found.


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

Anyone else have any thoughts on this please?

Is it a bug or is it something I'm doing..

Thanks

Bugreport was filed with the possible solution and JP will integrate it in the upcoming BETA ..

;)


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Anyone else have any thoughts on this please?

Is it a bug or is it something I'm doing..

Thanks

Hi. First comment on this forum.

I've used AutoIt for maybe a year now, and it's great. However, I just today coded a script that's supposed to compare files on your harddrive. If two files are of the exact same size, they will be written to a text-file. After running the program you can read the text-file, and locate files that might be duplicates.

To do this, I use a recursive function that's called with the name of the directory it should check. If the function stumbles on a folder, it calls itself with the folder name. Now, it's quoting time. Here's what the help-file says about the command FileOpen:

"Up to 64 files can be open simultaneously by one AutoIt script."

I've checked my code with MsgBox:es, and all the "$whatever = FileFindFirstFile ( $somedirectory )" are "FileClose ( $whatever )";), and all the files I write to or read from are closed during functions as well. Still, the program ends if I check a folder that has more then 64 folders inside it.

And I don't mean that the folder tree is more then 64 folders deep. I mean that if I have a folder with 70 folders inside it, all in the same folder, my code won't work. So it seems "Up to 64 files can be opened during the course of one AutoIt script" would be a more correct way of explaining it.. The only logical reason for this that I could come up with was that the FileClose-function ins't doing what it's supposed to, so I came here. Any ideas anyone? Is it the same problem as the one explained above? Has it been solved in version 3.1?

If there's any use for it, I'm happy to post my code, just ask. However, if there's need for posting my code, I need to know one thing. Are <TAB> removed from the code? I've noticed that some code-segments are indented, and some are not on this forum. I'd like it for my code to be indented.

//David, Sweden

Edited by DavoGotland

Share this post


Link to post
Share on other sites

Is it the same problem as the one explained above? Has it been solved in version 3.1?

If there's any use for it, I'm happy to post my code, just ask. However, if there's need for posting my code, I need to know one thing. Are <TAB> removed from the code? I've noticed that some code-segments are indented, and some are not on this forum. I'd like it for my code to be indented.

There was a bug with Handles whenever FileFindFirst() encountered an Empty directory. This is fixed in 3.1.1.81.

Try your script with the latest BETA and see if this solves your issue...


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0