Jump to content
ardrac

Confused with error

Recommended Posts

ardrac

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

 

Share this post


Link to post
Share on other sites
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
ardrac

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.

 

Share this post


Link to post
Share on other sites
water

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 (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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

×