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

Re: Recursive script doesn't like spaces? by Torgeir

Torgeir
Thu Jan 27 14:25:54 CST 2005

Craig wrote:

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

It doesn't look like spaces is an issue, but special characters
could be (as you write to an ASCII file that doesn't accept Unicode
characters).

Create an Unicode file instead, replace
Set textfile = objFSO.CreateTextFile("c:\filelist.txt", True)

with
Set textfile = objFSO.CreateTextFile("c:\filelist.txt", True, True)


If that doesn't help, it would help us if you posted the exact
error message and what line it errs on.



--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: Recursive script doesn't like spaces? by Craig

Craig
Thu Jan 27 15:27:08 CST 2005

Thank you, Torgeir.
That helped, but I still get an error at the "For Each objSubFolder in
colSubFolders" line, and the error is: Error: 0x80041017, Code:
80041017. I've looked at the script again, and I'm stumped about what
it doesn't like. Suggestions?

Thank you, again,
Craig


Torgeir Bakken (MVP) wrote:
> Craig wrote:
>
> > 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.
> Hi
>
> It doesn't look like spaces is an issue, but special characters
> could be (as you write to an ASCII file that doesn't accept Unicode
> characters).
>
> Create an Unicode file instead, replace
> Set textfile = objFSO.CreateTextFile("c:\filelist.txt", True)
>
> with
> Set textfile = objFSO.CreateTextFile("c:\filelist.txt", True, True)
>
>
> If that doesn't help, it would help us if you posted the exact
> error message and what line it errs on.
>
>
>
> --
> torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
> Administration scripting examples and an ONLINE version of
> the 1328 page Scripting Guide:
> http://www.microsoft.com/technet/scriptcenter/default.mspx


Re: Recursive script doesn't like spaces? by Torgeir

Torgeir
Thu Jan 27 15:55:26 CST 2005

Craig wrote:

> Thank you, Torgeir.
> That helped, but I still get an error at the "For Each objSubFolder in
> colSubFolders" line, and the error is: Error: 0x80041017, Code:
> 80041017. I've looked at the script again, and I'm stumped about what
> it doesn't like. Suggestions?
Hi

As Steven wrote, 0x80041017 means "invalid query syntax" for
ExecQuery, so I would think it is the line

Set colSubFolders = objWMIService.ExecQuery ("ASSOCIATORS OF
{Win32_Directory.Name='" & strSubFolder & "'} " & "WHERE AssocClass
= Win32_Subdirectory " & "ResultRole = PartComponent")

that errs, and not the "For Each objSubFolder in colSubFolders" line.


Replace your sub ShowSubFolders with this version, and report back
on what folder name the script errs on:

'--------------------8<----------------------

Sub ShowSubFolders(strSubFolder)

On Error Resume Next

Set colSubFolders = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & strSubFolder & "'} " _
& "WHERE AssocClass = Win32_Subdirectory " & "ResultRole = PartComponent")

If Err.Number <> 0 Then
WScript.Echo "Error when connecting to folder: " & strSubFolder
WScript.Quit
End If

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

'--------------------8<----------------------


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: Recursive script doesn't like spaces? by Craig

Craig
Thu Jan 27 16:31:18 CST 2005

Outstanding, Torgeir! That solved my problem. Although, I must confess,
I've looked at the "before and after" code...and I don't see what was
wrong. What was the problem?

Thank you, again.
Craig


Re: Recursive script doesn't like spaces? by Torgeir

Torgeir
Thu Jan 27 16:36:12 CST 2005

Craig wrote:

> Outstanding, Torgeir! That solved my problem. Although, I must confess,
> I've looked at the "before and after" code...and I don't see what was
> wrong. What was the problem?
Hi

I don't know.

I just added error suppressing with "On Error Resume Next" before
the ExecQuery statement, and checking for error after it.


So you did not get an "Error when connecting to folder: " message
now?


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Re: Recursive script doesn't like spaces? by Craig

Craig
Thu Jan 27 16:53:23 CST 2005

Nope. It "worked like a charm". I'm glad you had a suggestion though,
because I find the concept of the script really handy, and I wouldn't
have found that solution "in a 100 years!"

Thank you, again.
Craig


Re: Recursive script doesn't like spaces? by Torgeir

Torgeir
Thu Jan 27 17:04:49 CST 2005

Craig wrote:

> Nope. It "worked like a charm". I'm glad you had a suggestion though,
> because I find the concept of the script really handy, and I wouldn't
> have found that solution "in a 100 years!"
Hi

Please try this version of the ShowSubFolders sub:

'--------------------8<----------------------
Sub ShowSubFolders(strSubFolder)

Set colSubFolders = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='" & strSubFolder & "'} " _
& "WHERE AssocClass = Win32_Subdirectory " & "ResultRole = PartComponent")

On Error Resume Next

For Each objSubFolder in colSubFolders

If Err.Number <> 0 Then
WScript.Echo "Quitting, error when connecting to folder: " _
& strSubFolder
WScript.Quit
End If
Err.Clear

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




--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx