我正在编写一个脚本来解析计算机上的所有用户配置文件并检查目录的内容。根据目录的内容,可能会删除某些文件夹。
对于每个用户,我使用_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
如果您需要澄清任何内容,请告诉我。
答案 0 :(得分:2)
没有理由@error
会不断返回0,所以稍微重写就应该解决这个问题。我会以稍微不同的方式解决它,我会检查@error
是0
(我们的文件夹是否存在)并检查以确保$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