Question

I'm writing a script to parse through all user profiles on a machine and check the contents of a directory. Depending on the contents of the directory, certain folders may be deleted. For each user, I'm using _FileListToArray to read in all folders within

C:\Users\<Username>\AppData\Roaming\schlumberger\Petrel. 

The IF statement which checks the value of @error is being used to catch any empty arrays (no folders contained within

C:\Users\<UserName>\AppData\Roaming\schlumberger\Petrel

or the directory does not exist). If I run the function below, @error contains a value of 0, yet the IF condition is being evaluated as true and therefore the loops exits. If for instance I change the IF condition to @error = 4 and not 4 or 1, everything works as intended until one of the user profiles is parsed which does not have the schlumberger directory. This gives the following output in the console:

Variable must be of type "Object".

If I create the directory for that particular user, everything works as intended.

Ultimately my issue is why does @error only ever return 0? yet if I evaluate the value of @error in an IF statement which is looking for 1 & 4, the statement is sometimes found to be true?

Func DeleteDirectories($userList)

If $debug = True Then ConsoleWrite(@CR & 'Function: DeleteDirectories')

;_ArrayDisplay($userList)
For $userElement in $userList
    ; debug ------------------------------------------------------------------------------
    ConsoleWrite(@CR & 'Number of user profiles found = ' & $userList[0])
    ConsoleWrite(@CR & '$userElement = ' & $userElement)
    ConsoleWrite(@CR & 'Debug: C:\Users\' & $userElement & '\AppData\Roaming\schlumberger\Petrel')
    ; debug ------------------------------------------------------------------------------

    Local $petrelFolders = _FileListToArray("C:\Users\" & $userElement & "\AppData\Roaming\schlumberger\Petrel", "*", 2)
    ;ConsoleWrite(@CR & '@error = ' & @error)
    ;ConsoleWrite(@CR & '$petrelFolders = ' & $petrelFolders)
    If @error = 4 Then
        ConsoleWrite(@CR & 'No folders have been found.')
        ConsoleWrite(@CR & '@error = ' & @error)
    Else
        For $petrelElement in $petrelFolders
            ConsoleWrite(@CR & '@error = ' & @error)
            ConsoleWrite(@CR & '$petrelElement = ' & $petrelElement)
            If $petrelElement >= "2013" Then
                ConsoleWrite(@CR & "C:\Users\" & $userElement & "\AppData\Roaming\schlumberger\Petrel\" & $petrelElement & " will be deleted")
                Local $fileDeleteCheck = DirRemove("C:\Users\" & $userElement & "\AppData\Roaming\schlumberger\Petrel\" & $petrelElement)
                ConsoleWrite(@CR & '$fileDeleteCheck = ' & $fileDeleteCheck)
            EndIf
        Next
    EndIf
Next

CreateDirectories($userList)

EndFunc

Please let me know if you need anything clarified.

Was it helpful?

Solution

There's no reason @error would return 0 constantly, so a little re-write should fix this. I'd of gone about it in a slightly different way, I'd check that @error is 0 (our folder exists) and check to make sure the first element of $petrelFolders is larger than 0 (this first element holds the number of directories found, so if 0, we don't want to loop through) - this gives us:

Func DeleteDirectories($userList)

If $debug = True Then ConsoleWrite(@CR & 'Function: DeleteDirectories')

;_ArrayDisplay($userList)
For $userElement in $userList
    ; debug ------------------------------------------------------------------------------
    ConsoleWrite(@CR & 'Number of user profiles found = ' & $userList[0])
    ConsoleWrite(@CR & '$userElement = ' & $userElement)
    ConsoleWrite(@CR & 'Debug: C:\Users\' & $userElement & '\AppData\Roaming\schlumberger\Petrel')
    ; debug ------------------------------------------------------------------------------

    Local $petrelFolders = _FileListToArray("C:\Users\" & $userElement & "\AppData\Roaming\schlumberger\Petrel", "*", 2)
    ;ConsoleWrite(@CR & '@error = ' & @error)
    ;ConsoleWrite(@CR & '$petrelFolders = ' & $petrelFolders)
    If @error = 0 And $petrelFolders[0] > 0 Then
        For $i = 0 To Ubound($petrelFolders) - 1 Step 1
            If $i <> 0 And $petrelFolders[$i] >= Int("2013") Then
                ConsoleWrite(@CR & "C:\Users\" & $userElement & "\AppData\Roaming\schlumberger\Petrel\" & $petrelFolders[$i] & " will be deleted")
            EndIf
        Next
    EndIf
Next

CreateDirectories($userList)

EndFunc
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top