views:

20

answers:

1

I have a VB Script (.vbs file) that is just a simple directory listing of a drive. It will the base of a drive back up script. But when running it as it is below, I am getting a Permission Denied error on some folder. What I need to find is what that folder is so I can figure out what the problem is with the folder.

The line that is giving the error is "For Each TempFolder In MoreFolders". So what I am trying to figure out is how to WScript.Echo the current path (objDirectory) if there is an error.

I am not sure if it matters much, but just in case, the error that I am getting is Permission Denied 800A0046 on line 12. So some folder, I do not know which one, is not letting me look inside.

  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
A: 

Try adding the On Error Resume Next statement before the For Each loop and checking the Err.Number property after the problematic line, like this:

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
Helen