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:
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
|
|
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:
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.
Listing 4.17 Recursively Enumerating Subfolders
|
|
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 | « Previous | Next » |