开发者

Script stops on protected files such as system files

开发者 https://www.devze.com 2022-12-20 02:36 出处:网络
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 protected files or bypass t开发者_

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 protected files or bypass t开发者_JAVA百科hem?

Set objFS=CreateObject("Scripting.FileSystemObject") 
WScript.Echo Chr(34) & "Full Path" &_ 
 Chr(34) & ","  & Chr(34) & "File Size" &_ 
 Chr(34) & ","  & Chr(34) & "File Date modified" &_ 
 Chr(34) & "," & Chr(34) & "File Date Created" &_ 
 Chr(34) & "," & Chr(34) & "File Date Accessed" & Chr(34) 
Set objArgs = WScript.Arguments 
strFolder = objArgs(0) 
Set objFolder = objFS.GetFolder(strFolder) 
Go (objFolder) 
Sub Go(objDIR) 
  If objDIR <> "\System Volume Information" Then 
    For Each eFolder in objDIR.SubFolders 
        Go eFolder 
    Next    
  End If 
    For Each strFile In objDIR.Files 
        WScript.Echo Chr(34) & strFile.Path & Chr(34) & "," &_ 
        Chr(34) & strFile.Size & Chr(34) & "," &_ 
        Chr(34) & strFile.DateLastModified & Chr(34) & "," &_ 
        Chr(34) & strFile.DateCreated & Chr(34) & "," &_ 
        Chr(34) & strFile.DateLastAccessed & Chr(34) 
    Next  
End Sub 

Then call it from the command line like this.

c:\test> cscript //nologo myscript.vbs "c:\" > "C:\test\Output.csv"


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


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


ensure the process has permissions. see


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

精彩评论

暂无评论...
验证码 换一张
取 消