ardrac Posted June 23, 2016 Posted June 23, 2016 Hi folks, just wondering if anyone can give me an idea on why this is happening. I have a script, code below, that basically reads a text file; ImageFilesAll.txt - Each line of this file is just a full path and filename. For each file in the txt file, it works out if the file is older than 1 year, and if so deletes it. I know the code is probably not efficient and is probably not how someone else would write it, but I do my best. The script has worked perfectly on around 700 machines, but it fails on around 50. With an Error on line 7222, Subscript used on non-accessible variable. All the machines are based on the same image, and should in affect be just about identical. Any thoughts? #include <Date.au3> ; Required for _DateDiff() #include <File.au3> $DateStamp = @YEAR & @MON & @MDAY $ListFile = "C:\kworking\ImageFilesAll.txt" $linenumber = "1" $NumberOfFiles = _FileCountLines($ListFile) ; Retrieve the number of lines in C:\kworking\AllImageFiles.txt $OldFilesLog = "c:\kworking\ImageFilesOlderThan1Year.txt" $NewFilesLog = "c:\kworking\ImageFilesNewerThanYear.txt" FileWrite($OldFilesLog, @CRLF & "Run Date: " & $DateStamp & @CRLF) FileWrite($NewFilesLog, @CRLF & "Run Date: " & $DateStamp & @CRLF) While $linenumber <= $NumberOfFiles $filename = FileReadLine ($ListFile, $linenumber) ;Check if the current file is older than 1 days. If _IsFileOlder($filename, 365) Then FileWrite($OldFilesLog, $filename & " is being deleted" & @CRLF) FileDelete($filename) Else FileWrite($NewFilesLog, $filename & @CRLF) EndIf $linenumber = $linenumber + 1 WEnd ; Is a file older than a certain number of days. Func _IsFileOlder($sFilePath, $iDays) Local $aArray = FileGetTime($sFilePath, 0) Return _DateDiff('D', $aArray[0] & '/' & $aArray[1] & '/' & $aArray[2] & ' ' & $aArray[3] & ':' & $aArray[4] & ':' & $aArray[5], @YEAR & '/' & @MON & '/' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC) >= $iDays EndFunc ;==>_IsFileOlder
water Posted June 23, 2016 Posted June 23, 2016 I think you need to check if FileGetTime really returns an array. ; Is a file older than a certain number of days. Func _IsFileOlder($sFilePath, $iDays) Local $aArray = FileGetTime($sFilePath, 0) If @error Then Return 0 ; Ignore files that do not have the modified date set <== Line added Return _DateDiff('D', $aArray[0] & '/' & $aArray[1] & '/' & $aArray[2] & ' ' & $aArray[3] & ':' & $aArray[4] & ':' & $aArray[5], @YEAR & '/' & @MON & '/' & @MDAY & ' ' & @HOUR & ':' & @MIN & ':' & @SEC) >= $iDays EndFunc ;==>_IsFileOlder My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
ardrac Posted June 23, 2016 Author Posted June 23, 2016 Thanks, just tested, including the new line, on 1 of the problem machines and script went through with no errors and files deleted. So for my own understanding it was bombing out when it checked the time on a file, and if that didn't have a modified date set it failed to build the array. Makes sense. Thanks again.
water Posted June 23, 2016 Posted June 23, 2016 Exactly. We once had a similar problem with pictures downloaded from some digital cameras. When the date had not been set on the camera then all pictures would come in with date 1980-01-01. So our cleanup routine would delete them on the first run. So you might check for valid dates of your files (e.g. > 2013.01.01 and < current date) My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now