使用_FileListToArray的@error值

时间:2014-01-30 14:03:46

标签: arrays autoit

我正在编写一个脚本来解析计算机上的所有用户配置文件并检查目录的内容。根据目录的内容,可能会删除某些文件夹。 对于每个用户,我使用_FileListToArray来读取

中的所有文件夹
C:\Users\<Username>\AppData\Roaming\schlumberger\Petrel. 

检查@error值的IF语句用于捕获任何空数组(

中没有包含文件夹
C:\Users\<UserName>\AppData\Roaming\schlumberger\Petrel

或目录不存在)。如果我运行下面的函数,@ error包含值0,但IF条件被评估为true,因此循环退出。例如,如果我将IF条件更改为@error = 4而不是4或1,则一切都按预期工作,直到解析了其中一个没有schlumberger目录的用户配置文件。这在控制台中提供以下输出:

Variable must be of type "Object".

如果我为该特定用户创建目录,则一切都按预期工作。

最终我的问题是为什么@error只返回0?然而,如果我在@error语句中评估IF的值,该语句正在寻找1&amp; 4,声明有时被认为是真的吗?

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

如果您需要澄清任何内容,请告诉我。

1 个答案:

答案 0 :(得分:2)

没有理由@error会不断返回0,所以稍微重写就应该解决这个问题。我会以稍微不同的方式解决它,我会检查@error0(我们的文件夹是否存在)并检查以确保$petrelFolders的第一个元素更大比0(第一个元素保存找到的目录数,所以如果为0,我们不想循环) - 这给了我们:

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