Hi folks,
I have a handy script that recursively searches and then prints file
names within directories and their subdirectories. Unfortunately, the
script bombs sometimes...I "think" (because I'm new to scripting) that
it doesn't like directories with spaces or special characters in their
names. Suggestions? Here's the script below.
Thanks,
Craig
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set textfile = objFSO.CreateTextFile("c:\filelist.txt", True)
strComputer = "."
strDrive = "c:"
'NOTE: If you want to, you can provide a directory (like WINNT, etc.)
below in the
'parenthesis or leave it blank to search your whole hard drive!
strPath = "" 'Use double backslashes in path if necessary
strPathAlt = Replace(strPath, "\\", "\")
strName = strDrive & "\" & strPathAlt
Set objWMIService = GetObject("winmgmts:\\" & strComputer)
textfile.WriteLine strName
Set colFiles = objWMIService.ExecQuery ("SELECT * FROM CIM_DataFile
WHERE Drive = '" & strDrive & "' AND Path = '\\" & strPath & "\\'")
For Each objFile in colFiles
textfile.WriteLine objFile.Name
Next
Set colFolders = objWMIService.ExecQuery ("ASSOCIATORS OF
{Win32_Directory.Name='" & strName & "'} " & "WHERE AssocClass =
Win32_Subdirectory " & "ResultRole = PartComponent")
For Each objFolder in colFolders
textfile.WriteLine objFolder.Name
strFolderPath = Replace(objFolder.Path, "\", "\\")
strFilePath = strFolderPath & objFolder.FileName & "\\"
Set colFiles = objWMIService.ExecQuery ("SELECT * FROM CIM_DataFile
WHERE Drive = '" & strDrive & "' AND Path = '" & strFilePath & "'")
For Each objFile in colFiles
textfile.WriteLine objFile.Name
Next
ShowSubFolders objFolder.Name
Next
Sub ShowSubFolders(strSubFolder)
Set colSubFolders = objWMIService.ExecQuery ("ASSOCIATORS OF
{Win32_Directory.Name='" & strSubFolder & "'} " & "WHERE AssocClass =
Win32_Subdirectory " & "ResultRole = PartComponent")
For Each objSubFolder in colSubFolders
textfile.WriteLine objSubfolder.Name
strSubFolderPath = Replace(objSubFolder.Path, "\", "\\")
strSubFilePath = strSubFolderPath & objSubFolder.FileName & "\\"
Set colSubFiles = objWMIService.ExecQuery ("SELECT * FROM
CIM_DataFile WHERE Drive = '" & strDrive & "' AND Path = '" &
strSubFilePath & "'")
For Each objSubFile in colSubFiles
textfile.WriteLine objSubFile.Name
Next
ShowSubFolders objSubFolder.Name
Next
End Sub
textfile.Close
wscript.echo "Done. Have a nice day!"