mclark

Subscript error

8 posts in this topic

Hi. I know this question has been asked too many times to count, but I'm still asking. :idiot:

I am doing error checking (I thought) in my script, but yet I still get the infamous "Error: Subscript used on non-accessible variable" error on the script below. I have included it for your perusal. If someone could enlighten me as to where I'm missing something, I would appreciate it. The script fails in only certain situations (mostly on newly created VDI desktops), and runs fine most of the time. Thanks!

#Include <File.au3>
#Include <Array.au3>
#include <Date.au3>

If $CmdLine[0] > 0 Then
    If $CmdLine[1] = "DEBUG" Then
        $Debug=1
    Else
        $Debug=0
    EndIf
Else
    $Debug=0
EndIf

Global Const $LOCAL_APPLICATION_DATA = 0x1C
Global Const $APPLICATION_DATA = 0x1A
Global Const $MY_RECENT_DOCUMENTS = 0x8

Func EmptyFolder($FolderToDelete, $SkipNewData = False, $FilesOnly = False)
    If $FilesOnly Then
        $AllFiles = _FileListToArray($FolderToDelete,"*",1)
    Else
        $AllFiles = _FileListToArray($FolderToDelete,"*",0)
    EndIf
    If @Error Then
        If @Error <> 4 Then  ;-- This are no files found. Don't care about this error.
            If $Debug Then
                ConsoleWrite("ERROR!! On folder " & $FolderToDelete & @CRLF & "Error code: " & @Error )
                MsgBox(262144,'Information','Folder: ' & $FolderToDelete & @CRLF & 'Error code: ' & @Error, 5)
            EndIf
        EndIf
        Return
    EndIf
    If $Debug Then
        ConsoleWrite("-->" & $FolderToDelete & @CRLF )
        MsgBox(262144,'Information','Folder to delete: ' & $FolderToDelete, 5)
    EndIf
    If IsArray($AllFiles) Then
        If $Debug Then
            _ArrayDisplay( $AllFiles,$FolderToDelete)
        EndIf
        For $i = 1 To $AllFiles[0]
            $crt = FileGetTime( $FolderToDelete & "\" & $AllFiles[$i], 1 )
            If ($crt[2] = @MDAY And $crt[0] = @YEAR And $crt[1] = @MON) And $SkipNewData Then
                If $Debug Then
                    ConsoleWrite( $FolderToDelete & "\" & $AllFiles[$i] & " --> Today's File, Skipping!" & @CRLF )
                EndIf
                ContinueLoop
            EndIF
            $delete = FileDelete($FolderToDelete & "\" & $AllFiles[$i])
            If $Debug Then
                ConsoleWrite($FolderToDelete & "\" & $AllFiles[$i]& " =>"&$delete & @CRLF  )
            EndIf
            DirRemove($FolderToDelete & "\" & $AllFiles[$i], 1)
        Next
    EndIf
EndFunc

;---------- Clear Firefox cache
; Get the path to the Firefox cache folder
$FilePath = _FileFindSpecialFolder($LOCAL_APPLICATION_DATA, 0, 0)
$FilePath = $FilePath & "\Mozilla\Firefox\Profiles\"
$Search = FileFindFirstFile($FilePath & "*.default")
$FilePath = $FilePath & FileFindNextFile($Search) & "\Cache"
EmptyFolder ($FilePath, False, True)

;---------- Clear Chrome cache
; Get the path to the Chrome cache folder
$FilePath = _FileFindSpecialFolder($LOCAL_APPLICATION_DATA, 0, 0)
$FilePath = $FilePath & "\Google\Chrome\User Data\Default\"
FileDelete($FilePath & "Cookies")
FileDelete($FilePath & "Cookies-journal")
FileDelete($FilePath & "Current Session")
FileDelete($FilePath & "History")
FileDelete($FilePath & "History-journal")
$FilePath = $FilePath & "Cache"
EmptyFolder ($FilePath, False, True)

; ---- Clear IE temporary files
If $Debug Then
    MsgBox(262144,'Information','Clearing IE Temp Files', 5)
EndIf
ShellExecuteWait("RunDll32.exe"," InetCpl.cpl,ClearMyTracksByProcess 8")
;ShellExecuteWait("RunDll32.exe"," InetCpl.cpl,ClearMyTracksByProcess 255")

; ---- Remove temporary files
$FilePath = _FileFindSpecialFolder($LOCAL_APPLICATION_DATA, 0, 0)
EmptyFolder ($FilePath & "\Temporary Internet Files\Content.IE5")
EmptyFolder ($FilePath & "\Temporary Internet Files")
EmptyFolder ($FilePath & "\History")
EmptyFolder ($FilePath & "\Temp", True)
;;EmptyFolder ($FilePath & "\Application Data\Microsoft\Office\Recent")

; ---- Remove temporary files (XP)
$FilePath = "C:\Documents and Settings\" & @Username & "\Local Settings\Temp"
EmptyFolder ($FilePath, True)

; ---- Remove Windows directory temporary files
EmptyFolder (@WindowsDir & "\Temp", True)

; ---- Remove recent documents
;$FilePath = _FileFindSpecialFolder($MY_RECENT_DOCUMENTS, 0, 0)
;EmptyFolder ($FilePath)

;---------- Clear Recycle Bin (we'll only do this on certain day(s))
If _DateDayOfWeek( @WDAY ) = "Monday" Then
    FileRecycleEmpty()
EndIf

; ------------------------------------------------------------------------------------------------------------------
;===============================================================================
;
; Function Name:    _FileFindSpecialFolder($Folder,[ $ListToArray = 0[, $Filter = 0]])
; Description:
; Parameter(s):     $Folder      - The Special folder constant (i.e. $MY_COMPUTER)
;                                  You could also use an existing path (i.e. "C:\Program Files")
;                                  Or a ClassID (i.e. ::{20D04FE0-3AEA-1069-A2D8-08002B30309D} )
;                   $ListToArray - (optional) Change the return value:
;                                             0 = Return the ClassID or Path to the special folder
;                                             1 = Return an array containing the files within the folder
;                   $Filter      - (optional) Filter out the array by:
;                                       1 = Files
;                                       2 = Folders
;                                       3 = Links
;                                       4 = ClassId's
;                                       0 = No Filter (default)
; Requirement(s):   None.
; Return Value(s):  Returns either:
;                       1) A string containing the CLASSID or Path corresponding to the Special folder
;                       2) An Array containing the list of files/folder/links within the special folder
;                   Returns -1 if there are no results
; Author(s):        _Kurt
; Note(s):          See http://www.microsoft.com/technet/scriptcenter/scripts/desktop/special/default.mspx?mfr=true for more info
;
;===============================================================================

Func _FileFindSpecialFolder($Folder, $ListToArray = 0, $Filter = 0)
Local $oShell = ObjCreate("Shell.Application")

$nPath = $oShell.NameSpace($Folder).Self.Path
    If StringLeft($nPath,1) = ":" OR StringLeft($nPath,1) = ";" Then
        SetError(2)
    Else
        SetError(1)
    EndIf
    If $nPath = "" Then $nPath = -1
    If $ListToArray = 0 Then Return $nPath
    $nNum = $oShell.NameSpace($Folder).Items.Count
    Local $List[$nNum], $a = 0
    For $i = 0 To $nNum-1
        Select
            Case $Filter = 0
                $List[$i] = $oShell.NameSpace($Folder).Items.Item($i).Path
                $a += 1
            Case $Filter = 1
                If $oShell.NameSpace($Folder).Items.Item($i).IsFileSystem = True Then
                    If NOT $oShell.NameSpace($Folder).Items.Item($i).IsFolder = True Then
                        $List[$a] = $oShell.NameSpace($Folder).Items.Item($i).Path
                        $a += 1
                    EndIf
                EndIf
            Case $Filter = 2
                If $oShell.NameSpace($Folder).Items.Item($i).IsFolder = True Then
                    $List[$a] = $oShell.NameSpace($Folder).Items.Item($i).Path
                    $a += 1
                EndIf
            Case $Filter = 3
                If $oShell.NameSpace($Folder).Items.Item($i).IsLink = True Then
                    $List[$a] = $oShell.NameSpace($Folder).Items.Item($i).Path
                    $a += 1
                EndIf
            Case $Filter = 4
                $b = $oShell.NameSpace($Folder).Items.Item($i).Path
                If StringLeft($b, 1) = ":" OR StringLeft($b, 1) = ";" Then
                    $List[$a] = $oShell.NameSpace($Folder).Items.Item($i).Path
                    $a += 1
                EndIf
        EndSelect
    Next
    If $a = 0 Then
        $List = -1
    Else
        ReDim $List[$a]
    EndIf
    Return $List
EndFunc

Share this post


Link to post
Share on other sites



I copied and pasted your code into scite and ran it without issue. Can you give an example using the above udf that causes the error (using windows files)?


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

I don't know that I have a good example for you. A technician came and told me that the script was failing, and gave me a screenshot of the error. He said that it seems to happen on fresh virtual desktops that are being used for the first time. He said that once the desktop is logged into the first time, on second and subsequent logins the error doesn't happen. So I'm guessing that the files or folders don't exist at that point, but are created by the time the second login happens. I thought my script was set up to handle the errors, so I'm not sure why it isn't just skipping any problems that first time through.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

mclark,

There are 2 points where you do not check that an array is available:

- Line 43: FileGetTime

- Line 149/50: $nNum = $oShell.NameSpace($Folder).Items.Count -> Local $List[$nNum]

I suggest adding some debug code at those 2 points. ;)

M23

Edit: And welcome to the AutoIt forums. :)

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Thank you. I made a change to the lines 149/50 as you mentioned so we'll see if that helps.

My question is your comment about line 43. At line 38 I'm checking IsArray and stopping if it isn't, and if no files/directories are returned (i.e. empty array), line 42 should cause the loop not to execute (because it would be looping from 1 to 0, i.e. not looping at all), so I'm not sure how line 43 can be causing me a problem. I'm an AutoIt newbie, so take that into account.

mclark,

There are 2 points where you do not check that an array is available:

- Line 43: FileGetTime

- Line 149/50: $nNum = $oShell.NameSpace($Folder).Items.Count -> Local $List[$nNum]

I suggest adding some debug code at those 2 points. ;)

M23

Edit: And welcome to the AutoIt forums. :)

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Here's another one...validate this is an array before using it's subscripts:

$crt = FileGetTime( $FolderToDelete & "\" & $AllFiles[$i], 1 )
            If ($crt[2] = @MDAY And $crt[0] = @YEAR And $crt[1] = @MON) And $SkipNewData Then

so like this:

$crt = FileGetTime( $FolderToDelete & "\" & $AllFiles[$i], 1 )
            If IsArray($crt) And ($crt[2] = @MDAY And $crt[0] = @YEAR And $crt[1] = @MON) And $SkipNewData Then

or this:

$crt = FileGetTime( $FolderToDelete & "\" & $AllFiles[$i], 1 )
            If UBound($crt)<>5 And ($crt[2] = @MDAY And $crt[0] = @YEAR And $crt[1] = @MON) And $SkipNewData Then

or (best option):

$crt = FileGetTime( $FolderToDelete & "\" & $AllFiles[$i], 1 )
            If Not @error And ($crt[2] = @MDAY And $crt[0] = @YEAR And $crt[1] = @MON) And $SkipNewData Then
Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

mclark,

I have no proof that is the problem point, just that you are expecting an array at that point and do not check its existence before trying to access it. So it seems a possible location for the error you see. :)

By the way, when you reply, please use the "Reply to this topic" button at the top of the thread or the "Reply to this topic" editor at the bottom rather than the "Quote" button - I know what I wrote and it just pads the thread unnecessarily. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

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