gpt4 book ai didi

vbscript - VBScript捕获错误可变值

转载 作者:行者123 更新时间:2023-12-03 08:36:30 25 4
gpt4 key购买 nike

我有一个VB脚本(.vbs文件),它只是驱动器的简单目录列表。它是驱动器备份脚本的基础。但是,当按如下所示运行它时,我在某些文件夹上遇到了“权限被拒绝”错误。我需要找到的是该文件夹是什么,以便我可以找出该文件夹存在的问题。

出现错误的行是“对于MoreFolders中的每个TempFolder”。所以我想找出的是如何WScript.Echo当前路径(objDirectory),如果有错误。

我不确定这是否很重要,但是以防万一,我得到的错误是第12行上的Permission Denied 800A0046。因此,某些文件夹(我不知道是哪个文件夹)不允许我查看。

  Set WSShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject ("Scripting.FileSystemObject")

Dim FolderArr()
FolderCount = 0

TopCopyFrom = "G:\"

Sub WorkWithSubFolders(objDirectory)
Set MoreFolders = objDirectory.SubFolders
'The next line is where the error occurs (line 12)
For Each TempFolder In MoreFolders
FolderCount = FolderCount + 1
ReDim Preserve FolderArr(FolderCount)
FolderArr(FolderCount) = TempFolder.Path
' WScript.Echo TempFolder.Path
WorkWithSubFolders(TempFolder)
Next
End Sub

ReDim Preserve FolderArr(FolderCount)
FolderArr(FolderCount) = TopCopyFrom

Set objDirectory = objFSO.GetFolder(TopCopyFrom)
WorkWithSubFolders(objDirectory)
Set objDirectory = Nothing

WScript.Echo "FolderCount = " & FolderCount
WScript.Sleep 30000

Set objFSO = Nothing
Set WSShell = Nothing

最佳答案

尝试在On Error Resume Next循环之前添加 For Each 语句,并在有问题的行之后检查 Err.Number 属性,如下所示:

Sub WorkWithSubFolders(objDirectory)
On Error Resume Next
Set MoreFolders = objDirectory.SubFolders
For Each TempFolder In MoreFolders
If Err.Number = 0 Then
FolderCount = FolderCount + 1
ReDim Preserve FolderArr(FolderCount)
FolderArr(FolderCount) = TempFolder.Path
WorkWithSubFolders(TempFolder)
Else
WScript.Echo "Error #" & Err.Number & " " & Err.Description & vbNewLine & _
TempFolder.Path
Err.Clear
End If
Next
End Sub

关于vbscript - VBScript捕获错误可变值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2659071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com