Jump to content

Return of File functions (with wildcard)


Recommended Posts

Hello,

I have a problem with using some file functions (like filecopy). Sometimes, I think files are not yet totally copied and I can't access them.

I use a loop to ensure the function was successful.

I think that the problem is that I use wildcards so many files are selected at the same time.

In this case, what will return the functions, if some have been copied successfully, but not all.

Thanks for your replies

Link to comment
Share on other sites

FileCopy() will still only work with 1 file at a time when you use wildcards.

Put something Like

If @Error then ContinueLoop in the loop

BTW: Although it works, using wildcards in something like FileCopy() is not great and in FileMove() you will very probably get several files that don't get moved.

Get the files into an array and then work from the array.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Ok thank you!

So I'm probably going to use FileFind functions to make a filecopy of my own (accepting wildcards).

I found Siao's function : FileInUse which is very interesting and useful to know when a file you've just copied is accessible (to avoid unexpected error messages)! It is mainly useful when you copy files from a distant server: standard file functions seem to return even if the file is not accessible yet.

Link to comment
Share on other sites

There are also some other work-arounds you can look at.

$sFile = "SomeFile.txt"
$sFileSource = "C:\Some\Folder\"
$sFileDest = "C:\Another\Folder\"
$iCopySize = FileGetSize($sFileSouce & $sFile)

FileCopy($sFileSource & $sFile, $sFileDest & $sFile)
Do
   Sleep(1)
Until FileGetSize($sFileDest & $sFile) = $iCopySize

This is just one of a few possibilities and it's written on the fly so untested.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Thank you GeoSoft but I don't think it works because the file has a space preallocated before it is copied.

For example you can try to copy a big file from a server and with your method, try to open the file as soon as the sizes are the same and you'll see that it won't always work!

For information, I made my function like that:

; Check if a file is in use (by Siao)


Func _FileInUse($sFilename)
    ; author: Siao
    Local $aRet, $hFile
    $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _
    "str", $sFilename, _ ;lpFileName
    "dword", 0x80000000, _ ;dwDesiredAccess = GENERIC_READ
    "dword", 0, _ ;dwShareMode = DO NOT SHARE
    "dword", 0, _ ;lpSecurityAttributes = NULL
    "dword", 3, _ ;dwCreationDisposition = OPEN_EXISTING
    "dword", 128, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL
    "hwnd", 0) ;hTemplateFile = NULL
    $hFile = $aRet[0]
    If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1
        $aRet = DllCall("Kernel32.dll", "int", "GetLastError")
        SetError($aRet[0])
        Return 1
    Else
        ;close file handle
        DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
        Return 0
    EndIf
EndFunc ;==>_FileInUse


;-------------------------------------------------------------------------------


; Secured FileMove with wildcards (ensure all files are copied and accessible)


Func FileMove_for_sure($filename, $dirFilename, $destinationDir)
    If FileExists($dirFilename & $filename) Then
        $hsearch = FileFindFirstFile($dirFilename & $filename)
        If $hsearch <> -1 Then
            $currentfile = FileFindNextFile($hsearch)
            While Not @error
;~              try to move until it succeeds
                While (Not FileMove($dirFilename & $currentfile, $destinationDir, 1 + 8))
                    Sleep(1000)
                WEnd
                While _FileInUse($destinationDir & $currentfile)
                    Sleep(1000)
                WEnd
                $currentfile = FileFindNextFile($hsearch)
            WEnd
            FileClose($hsearch)
        EndIf
    EndIf
EndFunc   ;==>FileMove_for_sure

You can adapt it, for example using @extended to check it is a file or a folder!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...