nerds:~ #

20Jul/065

VBScript: Traverse Directories & Subdirectories Snippet

This snippet has come in handy quite a few times for me...

Call ListFolderContents("C:\Windows\System32\Drivers")

Sub ListFolderContents(path)
     set fs = CreateObject("Scripting.FileSystemObject")
     set folder = fs.GetFolder(path)
         Msgbox folder.path

         For each item in folder.SubFolders
                  ListFolderContents(item.Path)
         Next
     set folder = Nothing
     set fs = Nothing
End Sub
Posted by: Chrissy   Filed under: VBScript Leave a comment
Comments (5) Trackbacks (0)
  1. Well, it’s an ok script for reading all the directories, but it’s unruly. You don’t destroy the objects after instantiating them, and the msgboxes will get annoying popping up constantly without any way to break out of the script. maybe something that builds an array or dictionary object to store the names in and them does one msgbox with the complete list in it.

  2. Peter, that’s why it’s a snippet. This is a useless script on it’s own; you take it and do what you need to with it. Thanks for noticing that I didn’t kill the objects. I updated the script.

  3. Your script would be more efficient if you only created and destroyed the FileSystemObject once. I.e:

    Dim fs
    Set fs = CreateObject(“Scripting.FileSystemObject”)
    Call ListFolderContents(“C:\Windows\System32\Drivers”)
    Set fs = Nothing

    Sub ListFolderContents(path)
    set folder = fs.GetFolder(path)
    wscript.echo folder.path

    For each item in folder.SubFolders
    ListFolderContents(item.Path)
    Next
    set folder = Nothing
    End Sub

  4. Very nice script, thanks! I was having trouble understanding how to do recursive calls within VBScript…appreciate it :)


Leave a comment


No trackbacks yet.