下面的脚本拼凑在一起,循环遍历给定路径中的子文件夹和文件。对于每个文件,我抓取路径,文件名,文件大小,文件类型,所有者和与文件关联的日期。我有许多目录,我执行相同的任务,所以我创建了一个小批处理文件,调用vbs并将数据输出到文本文件(program.vbs echo>> output.txt)。
我的问题是,如何加入某种类型的错误处理?脚本在一个特定文件上失败,但该文件似乎没有损坏或具有任何独特的命名特征,所以我不知道它为什么失败。
任何输入都会非常感激,因为我确信我的脚本有很多缺陷,我宁愿学习正确的做事方式。
更新/解决方案:
Set fs = WScript.CreateObject ("Scripting.FileSystemObject")
Sub ShowSubFolders(Folder)
For Each Subfolder In Folder.SubFolders
On Error Resume Next
If Err Then
WScript.Echo "Error accessing " & folder & ": " & Err.Description
Err.Clear
Else
Set files = SubFolder.Files
For Each file In files
On Error Resume Next
If Err Then
WScript.Echo "Error accessing " & folder & ": " & Err.Description
Err.Clear
Else
strFilePath = File.Path
strFileName = File.Name
strFileSize = File.Size
strFileType = File.Type
strFileDateCreated = File.DateCreated
strFileDateLastAccessed = File.DateLastAccessed
strFileDateLastModified = File.DateLastModified
Set objWMIService = GetObject("winmgmts:")
Set objFileSecuritySettings = _
objWMIService.Get("Win32_LogicalFileSecuritySetting=""" & replace(file, "\", "\\") & """")
intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)
If intRetVal = 0 Then
strFileOwner = objSD.Owner.Domain & "\" & objSD.Owner.Name
Else
strFileOwner = "Couldn't retrieve security descriptor."
End If
Wscript.Echo strFilePath & "~" &_
strFileName & "~" &_
strFileSize & "~" &_
strFileType & "~" &_
strFileDateCreated & "~" &_
strFileDateLastAccessed & "~" &_
strFileDateLastModified & "~" &_
strFileOwner
End If
Next
ShowSubFolders Subfolder
End If
Next
End Sub
ShowSubFolders fs.GetFolder("C:\Test")
答案 0 :(得分:0)
请勿关闭,但在VBScript
错误处理中看起来很奇怪,以下是您的操作方法:
Dim i
' Turn on error Handling
On Error Resume Next
'Code here that you want to catch errors from
' Error Handler
If Err.Number <> 0 Then
' Error Occurred / Trap it
On Error Goto 0 ' But don't let other errors hide!
' Code to cope with the error here
End If
On Error Goto 0 ' Reset error handling.
如果这是一个ASP经典网站,有办法将IIS设置为创建xml错误日志文件。
答案 1 :(得分:0)
我有点改写你的剧本。如果它只是一个部分脚本然后公平,否则我认为它不会有效。事实上,如果它在“文件”上失败,它可能是最初引用的不存在的文件对象?
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Sub ShowSubFolders(folder)
On Error Resume Next
Dim subfolder, file
' Was the folder parameter a folder object? If not then Folder.Subfolders won't work. So substituting with the below:
For Each subfolder In fso.GetFolder(Folder).SubFolders
' If we have an error at this point then I can only imagine the specified folder doesn't exist or you don't have access to it
' Also where is "File.Path" etc coming from? I see no file objects at this point. So I'm removing all references to the file here.
If Err Then
WScript.Echo "Error accessing " & folder & ": " & Err.Description ' Assumes folder is a path rather than an object, otherwise should be folder.Path
Err.Clear ' Clear error for next loop
Else
For Each file In subfolder.Files
' Again if you get an error here then I can only see it being due to permissions, and either way if you
' errored on creating a file object then you won't have access to that file object's properties like file.Path
' etc, so again removing all of that. However I think it's extremely unlikely you'd get an error here anyway.
If Err Then
WScript.Echo "Error getting file object: " & Err.Description
Else
Dim strFilePath, strFileName, strFileSize, strFileType, strFileDateCreated, strFileDateLastAccessed, strFileDateLastModified
strFilePath = file.Path
strFileName = file.Name
strFileSize = file.Size
strFileType = file.Type
strFileDateCreated = file.DateCreated
strFileDateLastAccessed = file.DateLastAccessed
strFileDateLastModified = file.DateLastModified
' I'm not changing anything in the WMI section as I've never done security via WMI
Dim objWMIService, objFileSecuritySettings, intRetVal, strFileOwner
Set objWMIService = GetObject("winmgmts:")
Set objFileSecuritySettings = _
objWMIService.Get("Win32_LogicalFileSecuritySetting=""" & replace(file, "\", "\\") & """")
intRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD)
If intRetVal = 0 Then
strFileOwner = objSD.Owner.Domain & "\" & objSD.Owner.Name
Else
strFileOwner = "Couldn't retrieve security descriptor."
End If
Wscript.Echo strFilePath & "~" &_
strFileName & "~" &_
strFileSize & "~" &_
strFileType & "~" &_
strFileDateCreated & "~" &_
strFileDateLastAccessed & "~" &_
strFileDateLastModified & "~" &_
strFileOwner
End If
Err.Clear ' Clear the error for the next loop - putting this here since you aren't doing error
' checking in the WMI part; don't want to carry errors from here over into the next iteration
Next
End If
Next
On Error GoTo 0
End Sub
ShowSubFolders "C:\temp"