VBScript捕获错误变量值

时间:2010-04-17 16:18:59

标签: vbscript error-handling filesystemobject

我有一个VB脚本(.vbs文件),它只是一个驱动器的简单目录列表。它将驱动器备份脚本的基础。但是当它在下面运行时,我在某个文件夹上收到Permission Denied错误。我需要找到的是该文件夹是什么,所以我可以弄清楚该文件夹的问题。

给出错误的行是“For MoreFolders中的每个TempFolder”。所以我想弄清楚的是如果出现错误,如何WScript.Echo当前路径(objDirectory)。

我不确定它是否重要,但为了以防万一,我得到的错误是第12行的权限被拒绝800A0046。所以有些文件夹,我不知道哪一个,不让我看看里面。< / p>

  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

2 个答案:

答案 0 :(得分:1)

尝试在For Each循环之前添加On Error Resume Next语句,并在有问题的行后检查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

答案 1 :(得分:0)

看起来您正试图以某种方式打开“受保护”的文件夹。

如果“对于MoreFolders中的每个TempFolder”失败,则在捕获错误后进一步处理将不允许您通过检查“TempFolder.Path”的值来查看哪个文件夹失败。实际上,在错误处理代码中访问“TempFolder.Path”通常会导致未处理的错误,并且脚本将崩溃。

在进入“For Each”循环之前捕获文件夹的名称并捕获错误更加清晰:

    Option Explicit

    Dim objWSShell
    Dim objFSO
    Dim objDirectory

    Dim FolderCount
    Dim ErrFolderCount
    Dim TopCopyFrom
    Dim FolderArr()

    Dim strDirFullName
    Dim intDummy
    Dim intErrorNumber
    Dim strErrorDescription

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

        FolderCount = 0
        ErrFolderCount = 0

        TopCopyFrom = "C:\"

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

        WScript.Echo "Processing: " & FolderArr(FolderCount)

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

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

        If (ErrFolderCount > 0) Then
            WScript.Echo "ErrFolderCount = " & ErrFolderCount
        End If

        Set objFSO = Nothing
     '' Set objWSShell = Nothing

        WScript.Quit


    Sub WorkWithSubFolders(objDirectory)

    Dim MoreFolders
    Dim TempFolder

        strDirFullName = objDirectory.Path
     '' WScript.Echo "Processing: " & strDirFullName
        On Error Resume Next
            intDummy = objDirectory.SubFolders.Count
            intErrorNumber = Err.Number
            strErrorDescription = Err.Description
            Err.Clear
        On Error Goto 0
        If (intErrorNumber <> 0) Then
            WScript.Echo "Error #" & intErrorNumber & ",  " & strErrorDescription & ",  Processing folder: " & strDirFullName
            ErrFolderCount = ErrFolderCount + 1
            Exit Sub
        End If
        Set MoreFolders = objDirectory.SubFolders
        For Each TempFolder In MoreFolders
            FolderCount = FolderCount + 1
            ReDim Preserve FolderArr(FolderCount)
            FolderArr(FolderCount) = TempFolder.Path
            WorkWithSubFolders(TempFolder)
        Next
    End Sub

输出:

    Processing: C:\
    Error #70,  Permission denied,  Processing folder: C:\$Recycle.Bin\S-1-5-24-248752723-3714214951-6237883060-500
    Error #70,  Permission denied,  Processing folder: C:\$Recycle.Bin\S-1-5-24-192348782-4665637426-2144812622-1001
    Error #70,  Permission denied,  Processing folder: C:\$Recycle.Bin\S-1-5-24-192348782-4665637426-2144812622-1002
    Error #70,  Permission denied,  Processing folder: C:\$Recycle.Bin\S-1-5-24-192348782-4665637426-2144812622-1003
    Error #70,  Permission denied,  Processing folder: C:\$Recycle.Bin\S-1-5-24-192348782-4665637426-2144812622-500
    Error #70,  Permission denied,  Processing folder: C:\Documents and Settings
    Error #70,  Permission denied,  Processing folder: C:\MSOCache
    Error #70,  Permission denied,  Processing folder: C:\PerfLogs
    Error #70,  Permission denied,  Processing folder: C:\Program Files (x86)\Google\CrashReports
    Error #70,  Permission denied,  Processing folder: C:\Program Files (x86)\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA
    Error #70,  Permission denied,  Processing folder: C:\Program Files (x86)\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\JOBS
    Error #70,  Permission denied,  Processing folder: C:\Program Files (x86)\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Log
    ...
    ...
    ...
    Error #70,  Permission denied,  Processing folder: C:\Windows\Prefetch
    Error #70,  Permission denied,  Processing folder: C:\Windows\security\audit
    Error #70,  Permission denied,  Processing folder: C:\Windows\ServiceProfiles\LocalService
    Error #70,  Permission denied,  Processing folder: C:\Windows\ServiceProfiles\NetworkService
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\com\dmp
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\config
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\ias
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\LogFiles\Fax\Incoming
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\LogFiles\Fax\Outgoing
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\LogFiles\Firewall
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\LogFiles\HTTPERR
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\LogFiles\WMI
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\Msdtc
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\NetworkList
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\RsFx
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\spool\PRINTERS
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\Tasks
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\wbem\MOF
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\wdi
    Error #70,  Permission denied,  Processing folder: C:\Windows\System32\wfp
    Error #70,  Permission denied,  Processing folder: C:\Windows\SysWOW64\com\dmp
    Error #70,  Permission denied,  Processing folder: C:\Windows\SysWOW64\config
    Error #70,  Permission denied,  Processing folder: C:\Windows\SysWOW64\Msdtc
    Error #70,  Permission denied,  Processing folder: C:\Windows\SysWOW64\NetworkList
    Error #70,  Permission denied,  Processing folder: C:\Windows\SysWOW64\Tasks
    Error #70,  Permission denied,  Processing folder: C:\Windows\Temp
    FolderCount = 59815
    ErrFolderCount = 1109