Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - folder recursion not working in C:\Documents and Settings

Reply
 
Old 09-22-2009   #1 (permalink)
Tony Logan


 
 

folder recursion not working in C:\Documents and Settings

I copied a script straight from the Hey Scripting Guy! archives:
http://www.microsoft.com/technet/scr...5/hey0218.mspx.

It does exactly what I need it to do, which is start at a specified folder,
then recurse through all the sub-folders (and all the sub-sub-folders, etc.).
I've tested it on numerous folders and it works exactly as described.

Only problem is that it doesn't work if my starting folder is C:\Documents
and Settings. It throws a null error when it finds the first "Identities"
folder, like this:
C:\Documents and Settings\[user_name]\Application Data\Identities.

I've tested it on multiple PCs and it behaves the same.

Here's the code:

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

strFolderName = "c:\Documents and Settings"

Set colSubfolders = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")

Wscript.Echo strFolderName

arrFolderPath = Split(strFolderName, "\")
strNewPath = ""
For i = 1 to Ubound(arrFolderPath)
strNewPath = strNewPath & "\\" & arrFolderPath(i)
Next
strPath = strNewPath & "\\"

Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile where Path = '" & strPath & "'")

For Each objFile in colFiles
Wscript.Echo objFile.Name
Next

For Each objFolder in colSubfolders
GetSubFolders strFolderName
Next

Sub GetSubFolders(strFolderName)
Set colSubfolders2 = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")

For Each objFolder2 in colSubfolders2
strFolderName = objFolder2.Name
Wscript.Echo
Wscript.Echo objFolder2.Name
arrFolderPath = Split(strFolderName, "\")
strNewPath = ""
For i = 1 to Ubound(arrFolderPath)
strNewPath = strNewPath & "\\" & arrFolderPath(i)
Next
strPath = strNewPath & "\\"

Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_DataFile where Path = '" & strPath & "'")

For Each objFile in colFiles
Wscript.Echo objFile.Name
Next

GetSubFolders strFolderName
Next
End Sub


My System SpecsSystem Spec
Old 09-22-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: folder recursion not working in C:\Documents and Settings


"Tony Logan" <TonyLogan@newsgroup> wrote in message
news:7995F2EB-104B-4A41-B399-F0DF4A6E527C@newsgroup
Quote:

>I copied a script straight from the Hey Scripting Guy! archives:
> http://www.microsoft.com/technet/scr...5/hey0218.mspx.
>
> It does exactly what I need it to do, which is start at a specified
> folder,
> then recurse through all the sub-folders (and all the sub-sub-folders,
> etc.).
> I've tested it on numerous folders and it works exactly as described.
>
> Only problem is that it doesn't work if my starting folder is C:\Documents
> and Settings. It throws a null error when it finds the first "Identities"
> folder, like this:
> C:\Documents and Settings\[user_name]\Application Data\Identities.
>
> I've tested it on multiple PCs and it behaves the same.
>
> Here's the code:
>
> strComputer = "."
> Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
>
> strFolderName = "c:\Documents and Settings"
>
> Set colSubfolders = objWMIService.ExecQuery _
> ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
> & "Where AssocClass = Win32_Subdirectory " _
> & "ResultRole = PartComponent")
>
> Wscript.Echo strFolderName
>
> arrFolderPath = Split(strFolderName, "\")
> strNewPath = ""
> For i = 1 to Ubound(arrFolderPath)
> strNewPath = strNewPath & "\\" & arrFolderPath(i)
> Next
> strPath = strNewPath & "\\"
>
> Set colFiles = objWMIService.ExecQuery _
> ("Select * from CIM_DataFile where Path = '" & strPath & "'")
>
> For Each objFile in colFiles
> Wscript.Echo objFile.Name
> Next
>
> For Each objFolder in colSubfolders
> GetSubFolders strFolderName
> Next
>
> Sub GetSubFolders(strFolderName)
> Set colSubfolders2 = objWMIService.ExecQuery _
> ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
> & "Where AssocClass = Win32_Subdirectory " _
> & "ResultRole = PartComponent")
>
> For Each objFolder2 in colSubfolders2
> strFolderName = objFolder2.Name
> Wscript.Echo
> Wscript.Echo objFolder2.Name
> arrFolderPath = Split(strFolderName, "\")
> strNewPath = ""
> For i = 1 to Ubound(arrFolderPath)
> strNewPath = strNewPath & "\\" & arrFolderPath(i)
> Next
> strPath = strNewPath & "\\"
>
> Set colFiles = objWMIService.ExecQuery _
> ("Select * from CIM_DataFile where Path = '" & strPath & "'")
>
> For Each objFile in colFiles
> Wscript.Echo objFile.Name
> Next
>
> GetSubFolders strFolderName
> Next
> End Sub
>
This appears to be a permissions or locked folder issue. One way to deal
with it is to capture the error with an "On Error Resume Next" statement
just before the problem line. Another way is to use the File System Object.
The Scripting Guy mentions one advantage of using WMI: You can specify the
target machine. However, there is also a big drawback (other than your
current problem): WMI can be painfully slow. Try the code below to see what
I mean. Note also the simplicity of the code. If you need to access other
machines then you can do this by specifying a UNC path.

sFolder = "c:\Documents and Settings"
Set oFSO = CreateObject("Scripting.FileSystemObject")
GetFiles oFSO.GetFolder(sFolder)

Sub GetFiles(oFldr)
For Each oFile In oFldr.Files
WScript.Echo oFile.Path
Next

For Each oSubFolder In oFldr.SubFolders
GetFiles oSubFolder
Next
End Sub



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Vista - Documents & Settings Folder Vista file management
Specifying locatin of 'Documents And Settings' folder Vista installation & setup
Can't Access the Documents and Settings folder Vista General
Documents & Settings folder is not accessible! Vista General
Documents and Settings folder Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46