![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 > 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 Specs![]() |
![]() |
| 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 | |||