Enumerating Subfolders

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

In addition to knowing which files are stored in a folder, you need to know which subfolders are stored in a folder; this allows you to develop a complete picture of the folder infrastructure. The Folder object includes a Subfolders property that returns a collection consisting of the top-level subfolders for a folder.

Top-level subfolders are those folders contained directly within a folder; subfolders contained within those subfolders are not part of the collection. For example, in the sample folder structure shown in Figure 4.5, only Subfolder 1 and Subfolder 2 are top-level subfolders of the folder Scripts. As a result, only Subfolder 1 and Subfolder 2 are returned as part of the Subfolders property.

Figure 4.5   Sample Folder Structure

Sample Folder Structure

To obtain a subfolder collection, a script must:

  1. Create an instance of the FileSystemObject.
  2. Use the GetFolder method to bind to a folder.
  3. Create an object reference to the Subfolders property. This is required because collections are considered objects.

After you have obtained the object reference to the collection, you can then use a For Each loop to enumerate each of the subfolders in that collection. The script in Listing 4.16 binds to the folder C:\FSO and then echoes the name and size of each subfolder. In addition to the folder name, you can echo any of the folder properties shown in Table 4.3.

Listing 4.16   Enumerating Subfolders

1 2 3 4 5 6 
Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFolder = objFSO.GetFolder("C:\FSO") Set colSubfolders = objFolder.Subfolders For Each objSubfolder in colSubfolders     Wscript.Echo objSubfolder.Name, objSubfolder.Size Next

Enumerating Subfolders Within Subfolders

Depending on how your file system has been designed, simply knowing the top-level subfolders of a folder might provide sufficient information to map the folder infrastructure. In most file systems, however, folders are nested within folders that are, in turn, nested within other folders. The Subfolders collection can tell you that the folder C:\Accounting contains two subfolders: 2001 and 2002. However, it cannot tell which subfolders, if any, are contained within C:\Accounting\2001 and C:\Accounting\2002.

Fortunately, you can use recursion to enumerate all the subfolders within a set of subfolders. For example, the Subfolders collection, as shown in Listing 4.16, returns only the two top-level subfolders (Subfolder 1 and Subfolder 2) in the folder structure shown in Figure 4.6.

Figure 4.6   Sample Folder Structure

Sample Folder Structure

To return the complete set of subfolders (for example, Subfolder 1A and Subfolder 1B), you need to use a recursive function, a function that can call itself. For more information about recursion, see "VBScript Primer" in this book.

An example of a script that can enumerate all the subfolders of a folder (as well as any subfolders within those subfolders) is shown in Listing 4.17. This script:

  1. Creates an instance of the FileSystemObject.
  2. Uses the GetFolder method to bind to the folder C:\Scripts.

    GetFolder is used to return a folder object for C:\Scripts. In turn, the path C:\Scripts is passed as a parameter to the recursive subroutine ShowSubfolders. This subroutine will enumerate all the subfolders of C:\Scripts, as well as any subfolders within those subfolders.

  3. Retrieves a collection consisting of all the subfolders of the folder C:\Scripts. This collection has two items: Subfolder1 and Subfolder 2.
  4. Echoes the folder path of the first item in the collection, Subfolder 1. The subroutine then uses the name of that folder as a parameter passed to itself. In other words, the script now runs the subroutine ShowSubFolders using Subfolder 1 as the parameter.
  5. Retrieves a collection consisting of all the subfolders of Subfolder 1. This collection has two items: Subfolder1A and Subfolder 1B.
  6. Echoes the folder path of the first item in the collection, Subfolder 1A. The subroutine then uses the name of that folder as a parameter passed to itself. In other words, it now runs the function ShowSubFolders using Subfolder 1A as the parameter.
  7. Passes control to the next item in the collection, Subfolder 1B. This occurs because Subfolder 1A has no subfolders. The subroutine calls itself using Subfolder 1B as the parameter.
  8. Finishes recursing through Subfolder 1. This occurs because Subfolder 1B has no subfolders. The script then returns to the second item (Subfolder 2) in the original collection, and repeats the entire process.

Listing 4.17   Recursively Enumerating Subfolders

1 2 3 4 5 6 7 8 
Set FSO = CreateObject("Scripting.FileSystemObject") ShowSubfolders FSO.GetFolder("C:\Scripts") Sub ShowSubFolders(Folder)     For Each Subfolder in Folder.SubFolders         Wscript.Echo Subfolder.Path         ShowSubFolders Subfolder     Next End Sub

When this script runs under CScript, the following output appears in the command window:

C:\scripts\Subfolder 1 C:\scripts\Subfolder 1\Subfolder 1A C:\scripts\Subfolder 1\Subfolder 1B C:\scripts\Subfolder 2 C:\scripts\Subfolder 2\Subfolder 2A C:\scripts\Subfolder 2\Subfolder 2A\Subfolder 2A-1 C:\scripts\Subfolder 2\Subfolder 2B C:\scripts\Subfolder 2\Subfolder 2C 

To return a complete list of all the folders on a hard disk, begin the search in the root folder of the drive (for example, C:\).


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