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
--------------------
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
--------------------