Dim arrFolders()
intSize = 0
strRemoteComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
strFolderName = "c:\"
Set colSubfolders = objWMIService.ExecQuery _
("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
& "Where AssocClass = Win32_Subdirectory " _
& "ResultRole = PartComponent")
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
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
ReDim Preserve arrFolders(intSize)
arrFolders(intSize) = strFolderName
intSize = intSize + 1
GetSubFolders strFolderName
Next
End Sub
For Each strFolder in arrFolders
Wscript.Echo strFolder
Next
--
Scott A. Cooper
>I have a problem with the following script from the technet site. The
> error 0x80041017 pops up as it runs (local or remote) and if using
> the resume next statement, it will collect little over 500 items and
> then end. The error idicates a problem with the query but I have very
> little experiance working with associators so If someone could take a
> look, I would be grateful for your suggestions. Thank you.
Oh yeahh, you have created an engineering marvel. Let me explain YOUR
ERRORS! First you have to define all of your variables! Therefore, it
is the best, you use the Option Explicit at the beginning of every
script.
By the development of a program you should never switch off the error
handling. Error handling should be switched off only if at run-time
under circumstances an error could appear which can be catched in this
manner. A general switching off of the error handling by the interpreter
is not recommendable.
This requires of course an amount of discipline. Your computer is silly,
it expects from you correct instructions. This requires one clear
structure in every program. A PC not even owns the intelligence of a
mosquito or a fly.
>
> Dim arrFolders()
> intSize = 0
>
> strRemoteComputer = "."
> Set objWMIService = GetObject("winmgmts:\\" & strComputer &
> "\root\cimv2") strFolderName = "c:\"
Look here, that is wrong, the name of the variable is either
strRemoteComputer or strComputer. This error appears because you have
not defined your variables.
> Set colSubfolders = objWMIService.ExecQuery _
> ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
> & "Where AssocClass = Win32_Subdirectory " _
> & "ResultRole = PartComponent")
>
> ReDim Preserve arrFolders(intSize)
> arrFolders(intSize) = strFolderName
> intSize = intSize + 1
What should that be? A loop? No, that is no loop. That drives me crazy.
> For Each objFolder in colSubfolders
> GetSubFolders strFolderName
> Next
Oh, a loop! But no variable in the loop refers to the definition of the
variables in the head of the loop. If you tie together the bows of your
shoes, can you run then?
> Sub GetSubFolders(strFolderName)
Oh, this is a function. It creates an endless loop but it makes no
sense. Congratulation, you must be God, you have created the Eternity.
Define all of your variables you used in a function at the beginning of
the function for the scope of the function with the Dim statement. This
avoids to you annoyance and frustration.
> Set colSubfolders2 = objWMIService.ExecQuery _
> ("Associators of {Win32_Directory.Name='" & strFolderName &
> "'} " _ & "Where AssocClass = Win32_Subdirectory " _
> & "ResultRole = PartComponent")
>
> For Each objFolder2 in colSubfolders2
> strFolderName = objFolder2.Name
> ReDim Preserve arrFolders(intSize)
> arrFolders(intSize) = strFolderName
> intSize = intSize + 1
> GetSubFolders strFolderName
> Next
> End Sub
>
> For Each strFolder in arrFolders
> Wscript.Echo strFolder
> Next
This script could maybe fulfil your demands:
Option Explicit
Dim strComputer, wmi, strStartFolder
strComputer = "." ' variables with a global scope for the whole script
Set wmi = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
strStartFolder = "C:\"
WScript.Echo "Trying to display all of the subfolders of folder " & _
strStartFolder
Call DisplaySubFolders(strStartFolder)
Sub DisplaySubFolders(strFolder) ' Declaration of variables:
Dim colSubfolders, wmiFolder ' these variables become effective
' only in the scope of this subroutine
Set colSubfolders = wmi.ExecQuery("Associators of {" & _
"Win32_Directory.Name='" & strFolder & "'}" & _
" Where AssocClass = Win32_Subdirectory " & _
"ResultRole = PartComponent")
On Error Resume Next ' switching off error handling
For Each wmiFolder in colSubfolders
On Error GoTo 0 ' now switching on error handling
If Not IsEmpty(wmiFolder) Then ' catch a possible error
WScript.Echo wmiFolder.Name ' at run-time
Call DisplaySubFolders(wmiFolder.Name)
Else ' display the reason for the error
WScript.Echo "Variable wmiFolder is " & TypeName(wmiFolder)
WScript.Sleep 2000 ' that makes it readable
End If
Next
End Sub 'all of the variables defined in this function will be destroyed
--
ЯR