Enumerating the Subfolders of a Folder

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Instead of enumerating all the folders and subfolders on a computer, a more common task is examining the subfolders for a single folder. For example, you might have a folder named Users, and you might encourage your users to store their documents in this folder. Enumerating the subfolders within the Users folder can tell you which users have set up personal folders within that parent folder.

The Win32_Subdirectory class is an association class that allows you to associate a folder with its subfolders (or with its parent folder). Association classes typically have very few properties; their purpose is simply to derive the associations between objects. The Win32_Subdirectory class, for example, has only two properties:

  • GroupComponent. Returns the parent folder of a folder.
  • PartComponent. Returns the first-level subfolders of a folder.

To return a collection of subfolders for a folder, create an association query that sets the ResultRole to PartComponent. This indicates that all the items in the returned collection must play the role of a PartComponent, or subfolder, of the folder object. To return the parent folder for a folder, set the ResultRole to GroupComponent.

Figure 11.3 shows a sample folder structure. In this example, the associations for the folder Subfolder 1 are:

  • GroupComponent: Scripts.
  • PartComponent: Subfolder 1A; Subfolder 1B.

Figure 11.3   Sample Folder Structure

Sample Folder Structure

The Win32_Subfolder class works only on the file system level immediately above or immediately below the specified folder. If you retrieve the subfolders for the folder Scripts shown in Figure 11.3, the following two items are returned:

  • Subfolder 1
  • Subfolder 2

However, the subfolders of those subfolders (for example, Subfolder 1A and Subfolder 1B) are not included in the returned collection. This is because these folders are not directly contained within Scripts. To enumerate subfolders of subfolders, you need to create a recursive function that:

  1. Returns a collection of subfolders.
  2. Returns a collection of subfolders stored in each of those subfolders.
  3. Returns a collection of subfolders stored in each of those sub-subfolders.

The recursion continues until every subfolder has been queried.

Note

  • For information about creating recursive functions, see "VBScript Primer" in this book.

Scripting Steps

Listing 11.3 contains a script that returns a list of all subfolders within the folder C:\Scripts. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.
  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
  3. Use the ExecQuery method and an Associators of query to return a collection of all the subfolders within the folder C:\Scripts. The Associators of query requires the following parameters:
    • The source class, Win32_Directory, where Name equals C:\Scripts.
    • The associated class, Win32_Subdirectory. This limits data retrieval to folders. Files are not included in the returned collection.
    • The ResultRole, PartComponent, indicating that the items in the returned collection should be subfolders. Setting the ResultRole to GroupComponent would return the parent folder for C:\Scripts.
  4. For each subfolder in the collection, echo the folder name.

Listing 11.3   Enumerating the Subfolders of a Folder

1 2 3 4 5 6 7 8 9 10 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colSubfolders = objWMIService.ExecQuery _     ("ASSOCIATORS OF {Win32_Directory.Name='c:\scripts'} " _         & "WHERE AssocClass = Win32_Subdirectory " _             & "ResultRole = PartComponent") For Each objFolder in colSubfolders     Wscript.Echo objFolder.Name Next

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net