RSS

VBscript: Grabbing subfolders recursively

04 May

I was working on a script that needs me to grab a list of subfolders recursive off the user data area and found that vbscript language does not natively support a recursive function in its filesystemobject. So the first thing is to ask Mr. google who studiously brought up the pages I required. Obviously the Microsoft Scripting Center has the most referenced samples for this function.

Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Scripts")
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
  Wscript.Echo Subfolder.Path
  ShowSubFolders Subfolder
Next
End Sub

Sure this is a quick and easy sample, but most of the time, when we are looking for solution, we actually want to do something with the folders! I have seen many examples of how functions can be used and most of them are just simple and mostly useless immediately. Basically, if I want to display all subfolders in a folder, I don’t need to use vbscript, just “dir /b /s /a:d” will give me the same results!

For my solution, I needed to be able to grab each subfolder and work on them and it presents an interesting question because how do you store the list of subfolders in a recursive function? One of the ways is to use a global variable with a ByRef parameter. There are many ways of skinning this cat of course, I thought this is good enough for my solution, albeit sacrifice some coupling between functions. Just need to remember that if your subfolders list is exceedingly large, the string variable may hit this limit of 2 billion characters!

Set FSO = CreateObject("Scripting.FileSystemObject")
rf = "C:\Program Files"
SubFoldersList = ""
GetSubfolders rf, SubFoldersList
arrSubFolders = split(SubFoldersList, chr(9))
for each subf in arrSubfolders
  (do something)
next
Wscript.quit(0)
********************
Sub GetSubFolders(RootFolder, ByRef pSubfoldersList)
dim SubFolder, root
set root = fso.getfolder(RootFolder)
For Each Subfolder in root.SubFolders
  pSubFoldersList = pSubFoldersList & chr(9) & Subfolder.Path
  GetSubFolders Subfolder, pSubFoldersList
Next
End Sub

There are 2 caveats for the above script or even the sample given by Microsoft. If your folder string length is greater than 260, the line “for each subfolder…” will return path not found. This is a Microsoft limitation. I had to revert to “dir /s /b /a:d” to grab the list of subfolders instead, but still with the 260 limitation. The true work around is to map your unc path to a drive letter to shorten it. 🙂

Secondly, if you intend to write the folder path or name to a text file using FileSystemObject, you should remember to cater for unicode. Especially for us in Asian locations, unicode directory path names are very common.

Advertisements
 
Leave a comment

Posted by on May 4, 2010 in Scripts, VBScripts

 

Tags:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: