Apostrophes in a File Directory Traversal

Joined
Mar 4, 2009
Messages
1
Reaction score
0
Below is a recursive function for walking through a Directory Tree and writing Pathnames of all the files to a Text file.

I am working on Windows XP -- Professional and I have found that some of the names of my sub-folders contain apostrophes. So I am getting an error when I try to execute a query to get ASSOCIATORS of a Win32_Directory Object.

How do I Escape Apostrophe characters to instantiate a Win32_Directory Object?

--------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.CreateTextFile("C:\Temp\Files.lst")

GetSubFolders "G:\some_root_folder"

objTextFile.Close


Sub GetSubFolders(parmFolderName)
If parmFolderName > "" Then

' Collect Data Files in Passed Folder
Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & parmFolderName & "'} Where " _
& "ResultClass = CIM_DataFile")
' Traverse the set of Data Files and Output Pathnames to Image Files
For Each objFile in colFiles
objTextFile.WriteLine objFile.Name
Next



' Collect Folders in Passed Folder
Set colSubfolders2 = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & parmFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")

' Traverse the set of Sub-Folders and recursively Call GetSubFolders()
For Each objFolder2 in colSubfolders2
GetSubFolders objFolder2.Name
Next
Else
WScript.Echo "WARNING! empty parameter in GetSubFolders( " & parmFolderName & " )"
End If
End Sub
--------------------

 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top