Sign in to follow this  
Followers 0
Stalker0

Need help with copy verification

8 posts in this topic

#1 ·  Posted (edited)

After three hours I'm really stuck on this one guys, any help people could provide would be great.

What I'm doing is opening up a lotus notes program, making some alterations and saving the file. Lotus does the copying within the program, it doesn't provide any notification when the copying is finished, but I need to know when the copying is done before the script can continue.

I'm using the following code right now:

Do

$attribute = FileGetTime($FiletoCheck,0,1)

SLEEP(3000)

$attributenew = FileGetTime($FiletoCheck, 0, 1)

UNTIL $attribute = $attributenew

Its working right now, but if I drop the sleep timer down to 2000 sometimes it believes the file is finished copying before it actually is. I'm worried that with a very large file the timer may not be enough, and its just not an elegant way to do this.

I have tried comparing the file accessed time (which doesn't work because the script itself changes it), the file size (doesn't work, apprarantely the programs sets up the full file skeleton before it fills in all the data), and the file atrribute (always comes back as archive from start to finish).

Does anyone know a more elegant and reliable way to do this?

Also one more note, lotus must do the copying, as its making special alterations to the file I do not know how to do with another program. So I can't just outsource the copying to another program.

Edited by Stalker0

Share this post


Link to post
Share on other sites



Any ideas?

Share this post


Link to post
Share on other sites

Does lotus exit and close it's process after it is done doing what you want? Could always do a:

ProcessWaitClose('lotus')

Is it always writing to the file (meaning does it have the file open the whole time)... could always do a FileOpen() and check for @error then I suppose.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Lotus stays open when the file is finished so that idea is out.

Is there a way to generate the open file error without actually opening the file again? That idea might just work.

Share this post


Link to post
Share on other sites

Is there a way to generate the open file error without actually opening the file again?

How about a tall glass of confusion with that as well :) ... Seriously, the only way it's going to generate an error is if it's already open.

While _IsFileInUse($sPath)
    Sleep(500)
WEnd

Func _IsFileInUse($sPath);Return 0 if not in use
    Local $nError, $hOpen = FileOpen($sPath, 1)
    $nError = @error
    FileClose($hOpen)
    Return $nError
EndFunc
This isn't tested, but let me know if it works or not.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You can DllCall OpenFile API with OF_SHARE_EXCLUSIVE attribute set, which would throw an error if the file is already open for reading/writing.

While it probably would be more reliable than the method you currently use, it can hardly be called elegant :)

AutoIt's FileOpen/FileClose does not work for this purpose.

Edited by Siao

"be smart, drink your wine"

Share this post


Link to post
Share on other sites

You can DllCall OpenFile API with OF_SHARE_EXCLUSIVE attribute set, which would throw an error if the file is already open for reading/writing.

While it probably would be more reliable than the method you currently use, it can hardly be called elegant :)

AutoIt's FileOpen/FileClose does not work for this purpose.

Could you show me some code samples, I'm not sure how to set this one up.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Forum is full of examples and UDFs of how to use DllCall, using Windows API too. :)

Anyway, here you go...

$file = "FileInUseTestfile.txt"
$h = FileOpen($file, 1)
$x = _FileInUse($file)
MsgBox(0, "_FileInUse() example", "File "& $file & @CRLF & "Return= " & $x & "  (Error = " & @error & ")")
FileClose($h)
$x = _FileInUse($file)
MsgBox(0, "_FileInUse() example", "File "& $file & @CRLF & "Return= " & $x & "  (Error = " & @error & ")")
FileDelete($file)

;===============================================================================
;
; Function Name:    _FileInUse()
; Description:      Checks if file is in use
; Parameter(s):     $sFilename = File name
; Return Value(s):  1 - file in use (@error contains system error code)
;                   0 - file not in use
;
;===============================================================================
Func _FileInUse($sFilename)
    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
Edited by Siao

"be smart, drink your wine"

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