Q. How do I enumerate sub-folders in Visual Basic if the parent folder name contains apostrophes?

Jerold Schulman

December 18, 2006

1 Min Read
ITPro Today logo in a gray background | ITPro Today

To use a folder path that contains legal special characters in Visual Basic, enclose the folder path in double quotes ("), escaping every occurrence of a backslash ().

I have scripted enumFolderPath.vbs to accept a folder path, properly encapsulate it, escaping all backslash characters, and enumerate its' sub-folders.

The syntax for using enumFolderPath.vbs is:

cscript //nologo c:folderenumFolderPath.vbs Folder

Where Folder is the folder path, like c:file's or "C:Documents and SettingsJerryMy DocumentsMy script's".

enumFolderPath.vbs contains:

Dim objArgsSet objArgs = Wscript.Argumentsif objArgs.Count  1 then  Wscript.Echo  "Folder PATH required." WScript.Quit (1)End IfFldr = Replace(objArgs(0), "", "\")strComputer = "."Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\" & strComputer & "rootcimv2")Set colSubfolders = objWMIService.ExecQuery _ ("ASSOCIATORS OF {Win32_Directory.Name=

& Fldr &

} " _ & "WHERE AssocClass = Win32_Subdirectory " _ & "ResultRole = PartComponent")For Each objFolder in colSubfolders Wscript.Echo objFolder.NameNext


Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like