Sly01 Posted January 26, 2024 Posted January 26, 2024 (edited) Good Day Everyone, So I've started seeing the "Error: Subscript used on non-accessible variable" - As prescribed I'ved used Au3Stripper to find which line causes the problem. Unfortunately those are automated computers, so I cannot run the executables within SciTE to really pinpoint the error... But still, every time I checked it leads me to 1 of my functions I created. The goal of the Function is to list a folder content - Check for the dates then delete anything that is older then X. Below is the transcript of it, I've left as many things that I'm using in there so you have an idea of the general function. When I run AU3Stripper it would give me an error around: $fileTime = FileGetTime($Path & "\" & $aFileList[$i],0,0) So I had to rewrite this portion of the script that was "working" before, because I realised it was not deleting folders. The function before was using FindFileFirstFile and FindFileNextFile. So I started using _FileListToArray with different pattern. The first thing I understand I had to do - So the Variable is not empty was to use a @error to make sure that the _FileListToArray was not empty. So anyone can have an idea as to where I made a mistake that gives an empty array? expandcollapse popupDim $DelTempTimer = TimerInit() ; Variables for Folders Dim $MBKLogDir = "C:\MsgBoxKiller\logs" If Not FileExists ( $MBKLogDir ) Then DirCreate ( $MBKLogDir ) ; General Info Logs Func InfoLog( $Line = "" ) _FileWriteLog( $MBKLogDir & "\msgboxkiller-" & @YEAR & "-" & @MON & "-" & @MDAY & ".log", @ComputerName & " # [INFO] " & $Line) EndFunc ; Logging Function for File Deletion Func FileDeleteLog ( $Line = "" ) _FileWriteLog( $MBKLogDir & "\msgboxkiller-" & @YEAR & "-" & @MON & "-" & @MDAY & ".log", @ComputerName & " # [FILE DELETION] " & $Line ) EndFunc Func DelTemp($Path = @TempDir, $Pattern = "*.tmp", $MaxAge = 180) InfoLog("Starting Delete Temp File") $NowDate = _NowCalc() $aFileList = _FileListToArray($Path, $Pattern, 1) If Not @error Then If $aFileList[0] > 0 Then For $i = 1 to $aFileList[0] $fileTime = FileGetTime($Path & "\" & $aFileList[$i],0,0) $FileDate = $fileTime[0] & "/" & $fileTime[1] & "/" & $fileTime[2] & " " & $fileTime[3] & ":" & $fileTime[4] & ":" & $fileTime[5] If _DateDiff( 'n',$FileDate,$NowDate) > $MaxAge Then $delResult = FileDelete($Path & "\" & $aFileList[$i]) If $delResult = 1 Then FileDeleteLog("File " & $aFileList[$i] & " in folder " & $Path & " has been deleted.") EndIf EndIf Next EndIf Else FileDeleteLog("No files detected - Error Returned: " & @error) EndIf InfoLog("Starting Delete Temp Folder") $aDirList = _FileListToArray($Path, $Pattern, 2) If Not @error Then If $aDirList[0] > 0 Then For $i = 1 to $aDirList[0] $dirTime = FileGetTime($Path & "\" & $aDirList[$i],0,0) $dirDate = $dirTime[0] & "/" & $dirTime[1] & "/" & $dirTime[2] & " " & $dirTime[3] & ":" & $dirTime[4] & ":" & $dirTime[5] If _DateDiff( 'n',$dirDate,$NowDate) > $MaxAge Then $delResult = DirRemove($Path & "\" & $aDirList[$i],1) If $delResult = 1 Then FileDeleteLog("Folder " & $aDirList[$i] & " inside " & $Path & " has been deleted.") EndIf EndIf Next EndIf Else FileDeleteLog("No files detected - Error Returned: " & @error) EndIf EndFunc If TimerDiff ($DelTempTimer) >= 600000 Then InfoLog("[DELTEMP]Starting Empty DelTemp") DelTemp() ; Delete the *.tmp in @TempDir after 3 hours InfoLog("[DELTEMP]Starting MsgBoxKiller Logs cleanup") DelTemp($MBKLogDir, "*", 20160) ; Keep MsgBoxKiller Log file for 2 weeks InfoLog("[DELTEMP]Starting C:\Temp cleanup") DelTemp("C:\Temp", "*", 20160) ; Keep files in Temp folder for 2 Weeks for investigation in case of errors InfoLog("[DELTEMP]Starting Application Conversion Logs cleanup") DelTemp("C:\Application\Logs", "Convert2tiff*", 20160) ; Keeps File2Tiff logs for 2 weeks InfoLog("[DELTEMP]Starting Content.Word cleanup") DelTemp(@LocalAppDataDir & "\Microsoft\Windows\INetCache\Content.Word", "*") ; Delete Temp Folder after 3 hours InfoLog("[DELTEMP]Starting Content.MSO cleanup") DelTemp(@LocalAppDataDir & "\Microsoft\Windows\INetCache\Content.MSO", "*") ; Delete Temp Folder after 3 hours InfoLog("[DELTEMP]Starting Application Temp cleanup") DelTemp("C:\Application\temp", "*", 2880) ; Delete the Application Temp folder after 2 days $DelTempTimer = TimerInit() EndIf Edited January 26, 2024 by Sly01
Sly01 Posted January 26, 2024 Author Posted January 26, 2024 One thing might be worth mentioning the error happened so far when I call InfoLog("[DELTEMP]Starting Application Temp cleanup") DelTemp("C:\Application\temp", "*", 2880) ; Delete the Application Temp folder after 2 days Or InfoLog("[DELTEMP]Starting Empty DelTemp") DelTemp() ; Delete the *.tmp in @TempDir after 3 hours
Moderators Melba23 Posted January 26, 2024 Moderators Posted January 26, 2024 Sly01, When deleting from arrays, you should always start from the bottom and work up - otherwise you get an error as you try to access indices that no longer exist because earlier elements have been deleted from the array! So try using: For $i = $aFileList[0] To 1 Step -1 and see if that clears the problem. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Sly01 Posted January 26, 2024 Author Posted January 26, 2024 M23, Thanks for your reply, I'm not 100% sure I understand what you mean (I'm not a real Dev / Progammer so that might be a concept I don't understand) If I get what you're saying, is that when I delete a file, the array gets smaller? But I don't re-iterate the Array constantly. So the array stays the same until the end no? Why would the Array content changes if I don't recall the Array after every file delete - Sorry If I don't understand what you're trying to say, but I worked with Array before, and the Array should stay the same until the end, or not? The one thing I thought about, though, through your help, is that it COULD be possible that the file content changed WHILE I do my checks. So in the Array a certain file would be listed, but by the time it gets to the Deletion part that file does not exist anymore (as the automated process stills runs in the background) Should I add another check before process "$aFileList[i]" make sure that the file still exists?
Moderators Melba23 Posted January 26, 2024 Moderators Posted January 26, 2024 Sly01, Sorry, my bad based on a quick look at the code. I wrongly assumed you were deleting items from the array. I will take another more detailed look. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
ioa747 Posted January 26, 2024 Posted January 26, 2024 (edited) 2 hours ago, Sly01 said: "Error: Subscript used on non-accessible variable" With a quick look at the code If TimerDiff($DelTempTimer) >= 600000 Then $DelTempTimer it has not been declared yet but there are others that need correction, unless this is a piece, and not the entire script Edit: put the following line at the top of the script and press ctrl + f5 to check for error #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 Edited January 26, 2024 by ioa747 I know that I know nothing
Sly01 Posted January 26, 2024 Author Posted January 26, 2024 @ioa747 Thanks The $DelTempTimer I added it to the script above and it's in my normal script. My complete script has more than 1000 lines so I won't put the whole thing in here haha But the error really points to $fileTime = FileGetTime($Path & "\" & $aFileList[$i],0,0) Which I'm really wondering why XD...
Sly01 Posted January 26, 2024 Author Posted January 26, 2024 Hi Everyone Thanks for your help as usual!!!!! Always amazed!!! I found what the problem is... Because the files may have been in use once I create the Array... The files MAY be deleted by the time the Script get to the file. So when it goes through the FOR and gets to : $fileTime = FileGetTime($Path & "\" & $aFileList[$i],0,0) If the file has been treated and is now missing, it obviously cannot find the file so it cannot find the date. I've add a FileExists in the For before doing everything so something like: InfoLog("Starting Delete Temp File") Local $NowDate = _NowCalc() Local $aFileList = _FileListToArray($Path, $Pattern, 1) If Not @error Then If $aFileList[0] > 0 Then For $i = 1 to $aFileList[0] Local $iFileExists = FileExists($Path & "\" & $aFileList[$i]) If $iFileExists Then Local $fileTime = FileGetTime($Path & "\" & $aFileList[$i],0,0) Local $FileDate = $fileTime[0] & "/" & $fileTime[1] & "/" & $fileTime[2] & " " & $fileTime[3] & ":" & $fileTime[4] & ":" & $fileTime[5] If _DateDiff( 'n',$FileDate,$NowDate) > $MaxAge Then Local $delResult = FileDelete($Path & "\" & $aFileList[$i]) If $delResult = 1 Then FileDeleteLog("File " & $aFileList[$i] & " in folder " & $Path & " has been deleted.") EndIf EndIf EndIf Next EndIf Else FileDeleteLog("No files detected - Error Returned: " & @error) EndIf
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