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



November 27th, 2006 - 00:52
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.
November 27th, 2006 - 10:31
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.
March 1st, 2007 - 19:55
Thank you!
November 2nd, 2007 - 00:09
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
November 7th, 2008 - 19:00
Very nice script, thanks! I was having trouble understanding how to do recursive calls within VBScript…appreciate it