Script stops on protected files such as system files

前端 未结 4 1925
轮回少年
轮回少年 2021-01-26 11:19

This code stops after a while due to protected files such as system files, \"Permission Denied\".

Is there a way to modify the code below so that it can handle such prot

相关标签:
4条回答
  • 2021-01-26 11:59

    ensure the process has permissions. see

    0 讨论(0)
  • 2021-01-26 12:00

    You can ignore script errors in VBScript by adding

    On Error Resume Next
    

    before the part of the code where you want to ignore errors.

    The statement to restore the default behavior is

    On Error GoTo 0
    

    And just a remark: Method calls in VB and VBScript don't use parenthesis if they appear as a single statement. So the line Go (objFolder) should be replaced by Go objFolder.

    0 讨论(0)
  • 2021-01-26 12:07

    I've simplified your code (based upon your duplicate question) and without trying to handle errors I can see a problem: objDIR.SubFolders fails when one of the subfolders (such as \System Volume Information) doesn't have permissions to be viewed! You need to use another method on Folder to enumerate the foldernames, combine them with the existing path and then trap the error .GetFolder may cause when you don't have permissions. (I don't have time to code that solution at the moment.)

    Option Explicit
    
    Dim objFS
    Dim objArgs
    Dim strFolder
    Dim objFolder
    
    Set objFS = WScript.CreateObject("Scripting.FileSystemObject")
    WScript.StdOut.WriteLine """Full Path"",""File Size""," & _
      """File Date modified"",""File Date Created""," & _
      """File Date Accessed"""
    Set objArgs = WScript.Arguments
    strFolder = objArgs(0)
    Set objFolder = objFS.GetFolder(strFolder)
    Go objFolder
    Sub Go(objDIR)
      Dim strFile
      On Error Resume Next
        For Each eFolder in objDIR.SubFolders
            Go eFolder
        Next
        For Each strFile In objDIR.Files
            WScript.StdOut.WriteLine """" & strFile.Path & """,""" & _
              strFile.Size & """,""" & _
              strFile.DateLastModified & """,""" & _
              strFile.DateCreated & """,""" & _
              strFile.DateLastAccessed & """"
        Next
    End Sub
    
    0 讨论(0)
  • 2021-01-26 12:11

    VBScript allows error trapping, though not as gracefully as VBA. Try the script below.

    On Error Resume Next
        '[ ... code ...  ]
    Dim test_result, divisor
    
     divisor = 1        '' No error
    'divisor = 0        '' raise error #11
    'divisor = "zero"   '' raise a different error
    
    test_result = 2/divisor
    
    If Err.Number = 11 then  ''This line must appear at the point error is raised
        MsgBox "Handled Error: " & Err.Description
    ElseIf Err.Number > 0 then 
        MsgBox "Error: " & Err.Number & "  " & Err.Description
        Err.Clear   ''if you wanted to proceed clean from here
    End If
    
    MsgBox "Result: " & test_result
    
    0 讨论(0)
提交回复
热议问题