tags:

views:

78

answers:

4

I managed to get this code from you guys here, thanks so much with a collegue of mine, I ran this code a few times and it seems to work but stops after a while due to protected files such as system files, "Permission Denied" and then the script just stops, is there a way to modify the code below so that it can handle such protected files or whether it can pass them.

Any advice or help is appreciated.

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" 

Thank you :)

A: 

ensure the process has permissions. see

Sky Sanders
will give it a go and let you know of the outcome.Cheers
MalsiaPro
A: 

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.

0xA3
if i was to use this method to ignore errors then it will skill files, and that does not sound good, i need to ensure that all the files are processed. whether protected or not. I am using the RUNAS command but that is not working either. Any Suggestions guys.
MalsiaPro
What OS are you on? If you are on Vista/Windows 7 don't forget to run your script with elevation.
0xA3
@MalsiaPro If you want more control then just the Err object for errors. Ex: *If Err.Number = 70 Then*
Oorang
@MalsiaPro -- 'does not sound good' -- right you are! When you hide run-time errors, you make problems invisible ... which makes them very hard to solve. 'Resume Next' does have a use, but gets many coders into trouble.
Smandoli
+1  A: 

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
Mark Hurd
+1 that is bound to be helpful!
Smandoli
+1  A: 

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
Smandoli