Skip navigation

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

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:\folder\enumFolderPath.vbs Folder
Where Folder is the folder path, like c:\file's or "C:\Documents and Settings\Jerry\My Documents\My script's".

enumFolderPath.vbs contains:

Dim objArgs
Set objArgs = Wscript.Arguments
if objArgs.Count  1 then 
 Wscript.Echo  "Folder PATH required."
 WScript.Quit (1)
End If
Fldr = Replace(objArgs(0), "\", "\\")
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "\{impersonationLevel=impersonate\}!\\" & strComputer & "\root\cimv2")
Set colSubfolders = objWMIService.ExecQuery _
 ("ASSOCIATORS OF \{Win32_Directory.Name=
& Fldr &
\} " _ & "WHERE AssocClass = Win32_Subdirectory " _ & "ResultRole = PartComponent") For Each objFolder in colSubfolders Wscript.Echo objFolder.Name Next


Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish