Jump to content

Recommended Posts

Posted (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?
 

Dim $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 by Sly01
Posted

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
Posted

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted

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
Posted

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Posted (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 by ioa747

I know that I know nothing

Posted

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

Posted

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

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...