Jump to content

Skip copying file if file is locked


skysel
 Share

Recommended Posts

#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",",")

if Not FileExists("C:\Avayalogs\") then DirCreate("C:\Avayalogs\")

;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

;part 1
DirCreate("C:\Avayalogs\ay-aic01_temp\")

for $n =1 to UBound($myfile) -1
    RunWait(@ComSpec & " /c " & 'copy "' & $myfile[$n] &'" "C:\Avayalogs\ay-aic01_temp\"', "", @SW_HIDE);
Next

$Zip = _Zip_Create("C:\Avayalogs\" & $filename & ".zip")
       _Zip_AddFolder($Zip, "C:\Avayalogs\ay-aic01_temp\",4)
MsgBox(0,"",@error)

while _FileInUse("C:\Avayalogs\" & $filename & ".zip", 1)
    sleep(10)
WEnd

DirRemove("C:\Avayalogs\ay-aic01_temp\",1)



;part 2
DirCreate("C:\Avayalogs\ay-aic02_temp\")

for $n =1 to UBound($myfile2) -1
    RunWait(@ComSpec & " /c " & 'copy "' & $myfile2[$n] &'" "C:\Avayalogs\ay-aic02_temp\"', "", @SW_HIDE);
Next

$Zip = _Zip_Create("C:\Avayalogs\" & $filename2 & ".zip")
       _Zip_AddFolder($Zip, "C:\Avayalogs\ay-aic02_temp\",4)
MsgBox(0,"",@error)

while _FileInUse("C:\Avayalogs\" & $filename2 & ".zip", 1)
    sleep(10)
WEnd

DirRemove("C:\Avayalogs\ay-aic02_temp\",1)









;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

Link to comment
Share on other sites

Nice rework, heh :) I'll be able to report to you tomorrow if it still hangs. Meanwhile I removed error checking (both parts of script reported "0") after successfuly zipping the files.. it had to be removed because it's a scheduled task and you're not present at the computer to click "ok" :) If, however, script will still hang, i'm figuring that I could put in the command to self-terminate after xx minutes, so scheduler will be able to rerun the script (it doesnt hang always, just occasionaly).

Eitherway i'll let you know.

Link to comment
Share on other sites

Meanwhile I removed error checking (both parts of script reported "0") after successfuly zipping the files..

Upsa, that was just a leftover of my own testing :)... the temp dir's were not removed because the zip was still in creation, that's why I first looked up the @error from the zip and than used _FileInUse() to ensure the zip creation is finished...

Let me know, Cheers...

Link to comment
Share on other sites

So as it appears, it was unsuccessful. Script hanged again after whole day of perfect running. Error is the same:

Line -1: Error: Object referenced outside a "With" statement.

Addon: Was just browsing the forum right now, on how to make script kill itself after xx minutes have passed, but it turned out to be unsuccessful. a little pointer in which direction i should go for this? _Timer_SetTimer?

Edited by skysel
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...