How to store subfolder names in an array for later comparison -VBScript-

SnAkEhIpS
SnAkEhIpS used Ask the Experts™
on
The attached vbscript allows me to echo the base names of the top-level subfolders in the parent directory C:\Temp. I need to store them as elements of an array for comparison with a similar array. I have another script that I think will allow me to do the latter. How can I store the base names in an array?
'Enumerates all top-level folders by base name only, where the parent folder is C:\Temp

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

strFolderName = "C:\Temp"


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

For Each objFolder in colSubfolders
	strName = objFolder.name
	arrNames = Split(strName, "\")
	intIndex = Ubound(arrNames)

	Wscript.Echo arrNames(intIndex)

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2007
Commented:
Like this.
'Enumerates all top-level folders by base name only, where the parent folder is C:\Temp


strComputer = "."
Dim arrFolders()
i = 0
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

strFolderName = "C:\Temp"


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

For Each objFolder in colSubfolders
	strName = objFolder.name
	arrNames = Split(strName, "\")
	intIndex = Ubound(arrNames)
	Redim Preserve arrFolders (i)
 	arrFolders (i) = arrNames(intIndex)
	Wscript.Echo arrNames(intIndex)
	i = i + 1
Next

wscript.echo "Found the following folders..."
For each folderName in arrFolders
  wscript.echo folderName
Next

Open in new window

Top Expert 2010

Commented:
I'd be tempted to use a dictionary rather than an array.

Sirb: great seeing you back!
Top Expert 2007

Commented:
Or a d/c recordset, haha - thanks mate! :^)

Author

Commented:
Thank you very much for the rapid response. Works perfectly in conjuction with my other script.
Top Expert 2007

Commented:
Glad I could help - thanx for the grade! :^)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial