Jump to content

Skip copying file if file is locked


skysel
 Share

Recommended Posts

Good morning to all, I've looked around the forum but couldn't find anything related, so I'm asking for help here.. What happens in my script is, that when files are copied from network location, it appears that some files cannot be copied because they are locked by some proccess. How can I skip those files which are locked at the moment of copying?

This is the script..

#Include <File.au3>
#Include <String.au3>
#Include <Array.au3>
#Include <Zip.au3>

;Ay-Aic01
Dim $filename = "aic01." & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR & "-" & @MIN
Dim $myfile = StringSplit("\\Ay-aic01\logs\ADU_Voice1.log,\\Ay-aic01\logs\Alarm.log,\\Ay-aic01\logs\DataServerMSSQL_Default.log,\\Ay-aic01\logs\DataServerMSSQL_Email.log,\\Ay-aic01\logs\DataServerMSSQL_Voice1.log,\\Ay-aic01\logs\DataServerMSSQL_Voice1_Helper.log,\\Ay-aic01\logs\EDU_Email_Helper.log,\\Ay-aic01\logs\EDU_Voice1_Helper.log,\\Ay-aic01\logs\Report_Email_Helper.log,\\Ay-aic01\logs\Report_Voice1_Helper.log,\\Ay-aic01\logs\TSDefinity_Voice1.log,\\Ay-aic01\logs\TSQS_Voice1.log,\\Ay-aic01\logs\VOX_Voice1.log,\\Ay-aic01\logs\WorkFlow_Email.log,\\Ay-aic01\logs\WorkFlow_User1.log,\\Ay-aic01\logs\WorkFlow_Voice1.log",",")
;Ay-Aic02
Dim $filename2 = "aic02." & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR & "-" & @MIN
Dim $myfile2 = StringSplit("\\Ay-aic02\logs\ADU_Voice2.log,\\Ay-aic02\logs\Alarm_Core2.log,\\Ay-aic02\logs\DataServerMSSQL_Core2.log,\\Ay-aic02\logs\DataServerMSSQL_Voice2.log,\\Ay-aic02\logs\DataServerMSSQL_Voice2_Helper.log,\\Ay-aic02\logs\EDU_Voice2_Helper.log,\\Ay-aic02\logs\Report_Voice2_Helper.log,\\Ay-aic02\logs\TSDefinity_Voice2.log,\\Ay-aic02\logs\TSQS_Voice2.log,\\Ay-aic02\logs\VOX_Voice2.log,\\Ay-aic02\logs\WorkFlow_User2.log,\\Ay-aic02\logs\WorkFlow_Voice2.log",",")

Dim $Zip
DirCreate("C:\Avayalogs")

;Ay-Aic01
DirCreate("C:\ay-aic01")
for $n =1 to UBound($myfile) -1        
FileCopy($myfile[$n],"C:\ay-aic01")
Next

$Zip = _Zip_Create("C:\Avayalogs\" & $filename & ".zip")
       _Zip_AddFolder($Zip, "C:\ay-aic01",4)
 
FileDelete("C:\ay-aic01\*.log")
DirRemove("C:\ay-aic01")


;Ay-Aic02
DirCreate("C:\ay-aic02")
for $n =1 to UBound($myfile2) -1        
FileCopy($myfile2[$n],"C:\ay-aic02")
Next

$Zip = _Zip_Create("C:\Avayalogs\" & $filename2 & ".zip")
       _Zip_AddFolder($Zip, "C:\ay-aic02",4)
 
FileDelete("C:\ay-aic02\*.log")
DirRemove("C:\ay-aic02")
Link to comment
Share on other sites

good morning again :) so based on the link you provided, I modified script like this:

#Include <File.au3>
#Include <String.au3>
#Include <Array.au3>
#Include <Zip.au3>

;Ay-Aic01
Dim $filename = "aic01." & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR & "-" & @MIN
Dim $myfile = StringSplit("\\Ay-aic01\logs\ADU_Voice1.log,\\Ay-aic01\logs\Alarm.log,\\Ay-aic01\logs\DataServerMSSQL_Default.log,\\Ay-aic01\logs\DataServerMSSQL_Email.log,\\Ay-aic01\logs\DataServerMSSQL_Voice1.log,\\Ay-aic01\logs\DataServerMSSQL_Voice1_Helper.log,\\Ay-aic01\logs\EDU_Email_Helper.log,\\Ay-aic01\logs\EDU_Voice1_Helper.log,\\Ay-aic01\logs\Report_Email_Helper.log,\\Ay-aic01\logs\Report_Voice1_Helper.log,\\Ay-aic01\logs\TSDefinity_Voice1.log,\\Ay-aic01\logs\TSQS_Voice1.log,\\Ay-aic01\logs\VOX_Voice1.log,\\Ay-aic01\logs\WorkFlow_Email.log,\\Ay-aic01\logs\WorkFlow_User1.log,\\Ay-aic01\logs\WorkFlow_Voice1.log",",")
;Ay-Aic02
Dim $filename2 = "aic02." & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR & "-" & @MIN
Dim $myfile2 = StringSplit("\\Ay-aic02\logs\ADU_Voice2.log,\\Ay-aic02\logs\Alarm_Core2.log,\\Ay-aic02\logs\DataServerMSSQL_Core2.log,\\Ay-aic02\logs\DataServerMSSQL_Voice2.log,\\Ay-aic02\logs\DataServerMSSQL_Voice2_Helper.log,\\Ay-aic02\logs\EDU_Voice2_Helper.log,\\Ay-aic02\logs\Report_Voice2_Helper.log,\\Ay-aic02\logs\TSDefinity_Voice2.log,\\Ay-aic02\logs\TSQS_Voice2.log,\\Ay-aic02\logs\VOX_Voice2.log,\\Ay-aic02\logs\WorkFlow_User2.log,\\Ay-aic02\logs\WorkFlow_Voice2.log",",")
;zip
Dim $Zip
;FileInUse
Local $file = $myfile, $myfile2
Local $bFileOpen

While 1
    Sleep(10)
    for $n =1 to UBound($file) -1        
    Next
    $bFileOpen = _FileInUse($file[$n], 1)
    If $bFileOpen = -1 And $bFileOpen = 1 Then Exit
WEnd

;Script start
DirCreate("C:\Avayalogs")

;part 1
DirCreate("C:\ay-aic01")
for $n =1 to UBound($myfile) -1        
FileCopy($myfile[$n],"C:\ay-aic01")
Next

$Zip = _Zip_Create("C:\Avayalogs\" & $filename & ".zip")
       _Zip_AddFolder($Zip, "C:\ay-aic01",4)
 
FileDelete("C:\ay-aic01\*.log")
DirRemove("C:\ay-aic01")


;part 2
DirCreate("C:\ay-aic02")
for $n =1 to UBound($myfile2) -1        
FileCopy($myfile2[$n],"C:\ay-aic02")
Next

$Zip = _Zip_Create("C:\Avayalogs\" & $filename2 & ".zip")
       _Zip_AddFolder($Zip, "C:\ay-aic02",4)
 
FileDelete("C:\ay-aic02\*.log")
DirRemove("C:\ay-aic02")



;from this post:
;Need help with copy verification
;http://www.autoitscript.com/forum/index.php?showtopic=53994
;===============================================================================
;
; Function Name:    _FileInUse()
; Description:      Checks if file is in use
; Syntax.........: _FileInUse($sFilename, $iAccess = 1)
; Parameter(s):     $sFilename = File name
; Parameter(s):     $iAccess = 0 = GENERIC_READ - other apps can have file open in readonly mode
;                   $iAccess = 1 = GENERIC_READ|GENERIC_WRITE - exclusive access to file,
;                   fails if file open in readonly mode by app
; Return Value(s):  1 - file in use (@error contains system error code)
;                   0 - file not in use
;                   -1 dllcall error (@error contains dllcall error code)
; Author:           Siao
; Modified          rover - added some additional error handling, access mode
; Remarks           _WinAPI_CreateFile() WinAPI.au3
;===============================================================================
Func _FileInUse($sFilename, $iAccess = 0)
    Local $aRet, $hFile, $iError, $iDA
    Local Const $GENERIC_WRITE = 0x40000000
    Local Const $GENERIC_READ = 0x80000000
    Local Const $FILE_ATTRIBUTE_NORMAL = 0x80
    Local Const $OPEN_EXISTING = 3
    $iDA = $GENERIC_READ
    If BitAND($iAccess, 1) <> 0 Then $iDA = BitOR($GENERIC_READ, $GENERIC_WRITE)
    $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _
                                    "str", $sFilename, _ ;lpFileName
                                    "dword", $iDA, _ ;dwDesiredAccess
                                    "dword", 0x00000000, _ ;dwShareMode = DO NOT SHARE
                                    "dword", 0x00000000, _ ;lpSecurityAttributes = NULL
                                    "dword", $OPEN_EXISTING, _ ;dwCreationDisposition = OPEN_EXISTING
                                    "dword", $FILE_ATTRIBUTE_NORMAL, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL
                                    "hwnd", 0) ;hTemplateFile = NULL
    $iError = @error
    If @error Or IsArray($aRet) = 0 Then Return SetError($iError, 0, -1)
    $hFile = $aRet[0]
    If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1
        $aRet = DllCall("Kernel32.dll", "int", "GetLastError")
        ;ERROR_SHARING_VIOLATION = 32 0x20
        ;The process cannot access the file because it is being used by another process.
        If @error Or IsArray($aRet) = 0 Then Return SetError($iError, 0, 1)
        Return SetError($aRet[0], 0, 1)
    Else
        ;close file handle
        DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
        Return SetError(@error, 0, 0)
    EndIf
EndFunc

Is this ok? The script should exit if it detects file in use.

I dont want any verbose messages, thats why I removed error checking.

Edited by skysel
Link to comment
Share on other sites

Meh, the script doesn't work anyways, it says:

C:\Users\....\Desktop\Scripts\AvayaZipLog.au3 (73) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
$bFileOpen = _FileInUse($file[$n], 1)
$bFileOpen = _FileInUse(^ ERROR

further help please.. :)

Link to comment
Share on other sites

Looks like your $file variable is screwed.... I took the script from your first post (hope that it works) and added the marked code...

#Include <File.au3>
#Include <String.au3>
#Include <Array.au3>
#Include <Zip.au3>

;Ay-Aic01
Dim $filename = "aic01." & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR & "-" & @MIN
Dim $myfile = StringSplit("\\Ay-aic01\logs\ADU_Voice1.log,\\Ay-aic01\logs\Alarm.log,\\Ay-aic01\logs\DataServerMSSQL_Default.log,\\Ay-aic01\logs\DataServerMSSQL_Email.log,\\Ay-aic01\logs\DataServerMSSQL_Voice1.log,\\Ay-aic01\logs\DataServerMSSQL_Voice1_Helper.log,\\Ay-aic01\logs\EDU_Email_Helper.log,\\Ay-aic01\logs\EDU_Voice1_Helper.log,\\Ay-aic01\logs\Report_Email_Helper.log,\\Ay-aic01\logs\Report_Voice1_Helper.log,\\Ay-aic01\logs\TSDefinity_Voice1.log,\\Ay-aic01\logs\TSQS_Voice1.log,\\Ay-aic01\logs\VOX_Voice1.log,\\Ay-aic01\logs\WorkFlow_Email.log,\\Ay-aic01\logs\WorkFlow_User1.log,\\Ay-aic01\logs\WorkFlow_Voice1.log",",")
;Ay-Aic02
Dim $filename2 = "aic02." & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR & "-" & @MIN
Dim $myfile2 = StringSplit("\\Ay-aic02\logs\ADU_Voice2.log,\\Ay-aic02\logs\Alarm_Core2.log,\\Ay-aic02\logs\DataServerMSSQL_Core2.log,\\Ay-aic02\logs\DataServerMSSQL_Voice2.log,\\Ay-aic02\logs\DataServerMSSQL_Voice2_Helper.log,\\Ay-aic02\logs\EDU_Voice2_Helper.log,\\Ay-aic02\logs\Report_Voice2_Helper.log,\\Ay-aic02\logs\TSDefinity_Voice2.log,\\Ay-aic02\logs\TSQS_Voice2.log,\\Ay-aic02\logs\VOX_Voice2.log,\\Ay-aic02\logs\WorkFlow_User2.log,\\Ay-aic02\logs\WorkFlow_Voice2.log",",")

Dim $Zip
DirCreate("C:\Avayalogs")

;Ay-Aic01
DirCreate("C:\ay-aic01")
for $n =1 to UBound($myfile) -1
if _FileInUse($myfile[$n], 1) then continueloop ; ADDED
FileCopy($myfile[$n],"C:\ay-aic01")
Next

$Zip = _Zip_Create("C:\Avayalogs\" & $filename & ".zip")
       _Zip_AddFolder($Zip, "C:\ay-aic01",4)
 
FileDelete("C:\ay-aic01\*.log")
DirRemove("C:\ay-aic01")


;Ay-Aic02
DirCreate("C:\ay-aic02")
for $n =1 to UBound($myfile2) -1
if _FileInUse($myfile2[$n], 1) then continueloop ; ADDED       
FileCopy($myfile2[$n],"C:\ay-aic02")
Next

$Zip = _Zip_Create("C:\Avayalogs\" & $filename2 & ".zip")
       _Zip_AddFolder($Zip, "C:\ay-aic02",4)
 
FileDelete("C:\ay-aic02\*.log")
DirRemove("C:\ay-aic02")




;from this post:
;Need help with copy verification
;http://www.autoitscript.com/forum/index.php?showtopic=53994
;===============================================================================
;
; Function Name:    _FileInUse()
; Description:      Checks if file is in use
; Syntax.........: _FileInUse($sFilename, $iAccess = 1)
; Parameter(s):     $sFilename = File name
; Parameter(s):     $iAccess = 0 = GENERIC_READ - other apps can have file open in readonly mode
;                   $iAccess = 1 = GENERIC_READ|GENERIC_WRITE - exclusive access to file,
;                   fails if file open in readonly mode by app
; Return Value(s):  1 - file in use (@error contains system error code)
;                   0 - file not in use
;                   -1 dllcall error (@error contains dllcall error code)
; Author:           Siao
; Modified          rover - added some additional error handling, access mode
; Remarks           _WinAPI_CreateFile() WinAPI.au3
;===============================================================================
Func _FileInUse($sFilename, $iAccess = 0)
    Local $aRet, $hFile, $iError, $iDA
    Local Const $GENERIC_WRITE = 0x40000000
    Local Const $GENERIC_READ = 0x80000000
    Local Const $FILE_ATTRIBUTE_NORMAL = 0x80
    Local Const $OPEN_EXISTING = 3
    $iDA = $GENERIC_READ
    If BitAND($iAccess, 1) <> 0 Then $iDA = BitOR($GENERIC_READ, $GENERIC_WRITE)
    $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _
                                    "str", $sFilename, _ ;lpFileName
                                    "dword", $iDA, _ ;dwDesiredAccess
                                    "dword", 0x00000000, _ ;dwShareMode = DO NOT SHARE
                                    "dword", 0x00000000, _ ;lpSecurityAttributes = NULL
                                    "dword", $OPEN_EXISTING, _ ;dwCreationDisposition = OPEN_EXISTING
                                    "dword", $FILE_ATTRIBUTE_NORMAL, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL
                                    "hwnd", 0) ;hTemplateFile = NULL
    $iError = @error
    If @error Or IsArray($aRet) = 0 Then Return SetError($iError, 0, -1)
    $hFile = $aRet[0]
    If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1
        $aRet = DllCall("Kernel32.dll", "int", "GetLastError")
        ;ERROR_SHARING_VIOLATION = 32 0x20
        ;The process cannot access the file because it is being used by another process.
        If @error Or IsArray($aRet) = 0 Then Return SetError($iError, 0, 1)
        Return SetError($aRet[0], 0, 1)
    Else
        ;close file handle
        DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
        Return SetError(@error, 0, 0)
    EndIf
EndFunc
Edited by KaFu
Link to comment
Share on other sites

Looks like your $file variable is screwed.... I took the script from your first post (hope that it works) and added the marked code...

#Include <File.au3>
#Include <String.au3>
#Include <Array.au3>
#Include <Zip.au3>

;Ay-Aic01
Dim $filename = "aic01." & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR & "-" & @MIN
Dim $myfile = StringSplit("\\Ay-aic01\logs\ADU_Voice1.log,\\Ay-aic01\logs\Alarm.log,\\Ay-aic01\logs\DataServerMSSQL_Default.log,\\Ay-aic01\logs\DataServerMSSQL_Email.log,\\Ay-aic01\logs\DataServerMSSQL_Voice1.log,\\Ay-aic01\logs\DataServerMSSQL_Voice1_Helper.log,\\Ay-aic01\logs\EDU_Email_Helper.log,\\Ay-aic01\logs\EDU_Voice1_Helper.log,\\Ay-aic01\logs\Report_Email_Helper.log,\\Ay-aic01\logs\Report_Voice1_Helper.log,\\Ay-aic01\logs\TSDefinity_Voice1.log,\\Ay-aic01\logs\TSQS_Voice1.log,\\Ay-aic01\logs\VOX_Voice1.log,\\Ay-aic01\logs\WorkFlow_Email.log,\\Ay-aic01\logs\WorkFlow_User1.log,\\Ay-aic01\logs\WorkFlow_Voice1.log",",")
;Ay-Aic02
Dim $filename2 = "aic02." & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR & "-" & @MIN
Dim $myfile2 = StringSplit("\\Ay-aic02\logs\ADU_Voice2.log,\\Ay-aic02\logs\Alarm_Core2.log,\\Ay-aic02\logs\DataServerMSSQL_Core2.log,\\Ay-aic02\logs\DataServerMSSQL_Voice2.log,\\Ay-aic02\logs\DataServerMSSQL_Voice2_Helper.log,\\Ay-aic02\logs\EDU_Voice2_Helper.log,\\Ay-aic02\logs\Report_Voice2_Helper.log,\\Ay-aic02\logs\TSDefinity_Voice2.log,\\Ay-aic02\logs\TSQS_Voice2.log,\\Ay-aic02\logs\VOX_Voice2.log,\\Ay-aic02\logs\WorkFlow_User2.log,\\Ay-aic02\logs\WorkFlow_Voice2.log",",")

Dim $Zip
DirCreate("C:\Avayalogs")

;Ay-Aic01
DirCreate("C:\ay-aic01")
for $n =1 to UBound($myfile) -1
if _FileInUse($myfile[$n], 1) then continueloop ; ADDED
FileCopy($myfile[$n],"C:\ay-aic01")
Next

$Zip = _Zip_Create("C:\Avayalogs\" & $filename & ".zip")
       _Zip_AddFolder($Zip, "C:\ay-aic01",4)
 
FileDelete("C:\ay-aic01\*.log")
DirRemove("C:\ay-aic01")


;Ay-Aic02
DirCreate("C:\ay-aic02")
for $n =1 to UBound($myfile2) -1
if _FileInUse($myfile2[$n], 1) then continueloop ; ADDED       
FileCopy($myfile2[$n],"C:\ay-aic02")
Next

$Zip = _Zip_Create("C:\Avayalogs\" & $filename2 & ".zip")
       _Zip_AddFolder($Zip, "C:\ay-aic02",4)
 
FileDelete("C:\ay-aic02\*.log")
DirRemove("C:\ay-aic02")




;from this post:
;Need help with copy verification
;http://www.autoitscript.com/forum/index.php?showtopic=53994
;===============================================================================
;
; Function Name:    _FileInUse()
; Description:      Checks if file is in use
; Syntax.........: _FileInUse($sFilename, $iAccess = 1)
; Parameter(s):     $sFilename = File name
; Parameter(s):     $iAccess = 0 = GENERIC_READ - other apps can have file open in readonly mode
;                   $iAccess = 1 = GENERIC_READ|GENERIC_WRITE - exclusive access to file,
;                   fails if file open in readonly mode by app
; Return Value(s):  1 - file in use (@error contains system error code)
;                   0 - file not in use
;                   -1 dllcall error (@error contains dllcall error code)
; Author:           Siao
; Modified          rover - added some additional error handling, access mode
; Remarks           _WinAPI_CreateFile() WinAPI.au3
;===============================================================================
Func _FileInUse($sFilename, $iAccess = 0)
    Local $aRet, $hFile, $iError, $iDA
    Local Const $GENERIC_WRITE = 0x40000000
    Local Const $GENERIC_READ = 0x80000000
    Local Const $FILE_ATTRIBUTE_NORMAL = 0x80
    Local Const $OPEN_EXISTING = 3
    $iDA = $GENERIC_READ
    If BitAND($iAccess, 1) <> 0 Then $iDA = BitOR($GENERIC_READ, $GENERIC_WRITE)
    $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _
                                    "str", $sFilename, _ ;lpFileName
                                    "dword", $iDA, _ ;dwDesiredAccess
                                    "dword", 0x00000000, _ ;dwShareMode = DO NOT SHARE
                                    "dword", 0x00000000, _ ;lpSecurityAttributes = NULL
                                    "dword", $OPEN_EXISTING, _ ;dwCreationDisposition = OPEN_EXISTING
                                    "dword", $FILE_ATTRIBUTE_NORMAL, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL
                                    "hwnd", 0) ;hTemplateFile = NULL
    $iError = @error
    If @error Or IsArray($aRet) = 0 Then Return SetError($iError, 0, -1)
    $hFile = $aRet[0]
    If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1
        $aRet = DllCall("Kernel32.dll", "int", "GetLastError")
        ;ERROR_SHARING_VIOLATION = 32 0x20
        ;The process cannot access the file because it is being used by another process.
        If @error Or IsArray($aRet) = 0 Then Return SetError($iError, 0, 1)
        Return SetError($aRet[0], 0, 1)
    Else
        ;close file handle
        DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
        Return SetError(@error, 0, 0)
    EndIf
EndFunc
Now it reports that folder c:\ay-aic01 is empty so it cannot create any zip archive.. :s
Link to comment
Share on other sites

I see... my guess is, that _FileInUse() only works for local processes and you're working on a network drive...

what are the error codes, if you call them directly after the _FileInUse()?

_FileInUse($myfile[$n],0)
MsgBox(0,"",@error)

Took a look into the UDF's... maybe _Net_Share_FileGetInfo() is a better way to go?

Link to comment
Share on other sites

hmm...

if _FileInUse($myfile[$n], 0) And MsgBox(0,"",@error) then continueloop; ADDED

I modified this line so error appears. But there is no error. For each file it returns "0", which means:

Return Value(s): 1 - file in use (@error contains system error code)

0 - file not in use

So I assume this part of script is OK. Must be something wrong with FileCopy then...

Link to comment
Share on other sites

Guess the _FileInUse() is only valid for local operations... maybe _Net_Share_FileGetInfo() will help.

Maybe also take a look at the FileCopy() syntax by adding an msgbox

msgbox(0,"",$myfile[$n] & @crlf & ,"C:\ay-aic01")

Link to comment
Share on other sites

Right, happens if you try to code in "Fast Reply" :)

msgbox(0,"",$myfile[$n] & @crlf & "C:\ay-aic01")

Im not sure where to put this msgbox, as filecopy syntax doesnt support inside commands? I tried putting it before filecopy, and after filecopy. Both resulted in same error as before (no files can be found in the directory, so therefor archive cannot be created..)
Link to comment
Share on other sites

hmmm, tested the following on our server here...

#Include <File.au3>
#Include <String.au3>
#Include <Array.au3>
#Include <Zip.au3>

;Ay-Aic01
Dim $filename = "aic01." & @YEAR & "-" & @MON & "-" & @MDAY & "-" & @HOUR & "-" & @MIN
Dim $myfile = StringSplit("\\server\dir\test.gif,\\server\dir\test2.gif,\\server\dir\test.xls,",",")

Dim $Zip
DirCreate("C:\Avayalogs")

;Ay-Aic01
DirCreate("C:\ay-aic01")
for $n =1 to UBound($myfile) -1
    msgbox(0,"",$myfile[$n] & @crlf & "C:\ay-aic01")
    if _FileInUse($myfile[$n], 0) then 
        MsgBox(0,"",@error)
        ;continueloop ; ADDED
    EndIf
    FileCopy($myfile[$n],"C:\ay-aic01")
Next

$Zip = _Zip_Create("C:\Avayalogs\" & $filename & ".zip")
       _Zip_AddFolder($Zip, "C:\ay-aic01",4)
 
FileDelete("C:\ay-aic01\*.log")
DirRemove("C:\ay-aic01")




;from this post:
;Need help with copy verification
;http://www.autoitscript.com/forum/index.php?showtopic=53994
;===============================================================================
;
; Function Name:    _FileInUse()
; Description:      Checks if file is in use
; Syntax.........: _FileInUse($sFilename, $iAccess = 1)
; Parameter(s):     $sFilename = File name
; Parameter(s):     $iAccess = 0 = GENERIC_READ - other apps can have file open in readonly mode
;                   $iAccess = 1 = GENERIC_READ|GENERIC_WRITE - exclusive access to file,
;                   fails if file open in readonly mode by app
; Return Value(s):  1 - file in use (@error contains system error code)
;                   0 - file not in use
;                   -1 dllcall error (@error contains dllcall error code)
; Author:           Siao
; Modified          rover - added some additional error handling, access mode
; Remarks           _WinAPI_CreateFile() WinAPI.au3
;===============================================================================
Func _FileInUse($sFilename, $iAccess = 0)
    Local $aRet, $hFile, $iError, $iDA
    Local Const $GENERIC_WRITE = 0x40000000
    Local Const $GENERIC_READ = 0x80000000
    Local Const $FILE_ATTRIBUTE_NORMAL = 0x80
    Local Const $OPEN_EXISTING = 3
    $iDA = $GENERIC_READ
    If BitAND($iAccess, 1) <> 0 Then $iDA = BitOR($GENERIC_READ, $GENERIC_WRITE)
    $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _
                                    "str", $sFilename, _ ;lpFileName
                                    "dword", $iDA, _ ;dwDesiredAccess
                                    "dword", 0x00000000, _ ;dwShareMode = DO NOT SHARE
                                    "dword", 0x00000000, _ ;lpSecurityAttributes = NULL
                                    "dword", $OPEN_EXISTING, _ ;dwCreationDisposition = OPEN_EXISTING
                                    "dword", $FILE_ATTRIBUTE_NORMAL, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL
                                    "hwnd", 0) ;hTemplateFile = NULL
    $iError = @error
    If @error Or IsArray($aRet) = 0 Then Return SetError($iError, 0, -1)
    $hFile = $aRet[0]
    If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1
        $aRet = DllCall("Kernel32.dll", "int", "GetLastError")
        ;ERROR_SHARING_VIOLATION = 32 0x20
        ;The process cannot access the file because it is being used by another process.
        If @error Or IsArray($aRet) = 0 Then Return SetError($iError, 0, 1)
        Return SetError($aRet[0], 0, 1)
    Else
        ;close file handle
        DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
        Return SetError(@error, 0, 0)
    EndIf
EndFunc

The two gifs were not open and copied sucessfully, the xls i opend manually and the FileCopy was skipped due to the _FileInUse(). A zip containing the two gifs was created successfully... When i commented the _FileInUse() part, the open xls was copied sucessfully too.

So here some more guesses / questions...

- re-check the syntax of the target file-location (try to open them by copy&paste the address in the explorer address bar)...

- all of the files you want to copy ARE open and thus the copying process is skipped with reason...

- is it possible to manually copy the files?

- what is the EXACT failure behavior? Does the script finish? Throw error messages?

Link to comment
Share on other sites

OR alternative remove the _FileInUse() function and replace FileCopy() with

RunWait(@ComSpec & " /c " & 'copy "' & $myfile[$n] &'" "C:\ay-aic01"', "", @SW_HIDE);
Ah, excelent workaround. I will try it on monday and will let you know how it goes. As for _FileInUse, the script only reported that there are no files in directory which was supposed to be zipped. So there must have been problem with FileCopy - but what - is beyond my knowledge. Copy command automaticly skips the file if it is in use so thats the perfect solution for me. It doesnt pause the script - meanwhile with FileCopy and without FileInUse the script just paused for unknown reason, thats why I wanted FileInUse on first place. Hope it makes sense :)

Thanks for the help and I'll let you know how it goes!

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...