Enumerating All the Folders on a Computer

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

As demonstrated in the preceding script, WMI can be used to bind to and retrieve the properties from a specific file system folder. In addition, WMI can be used to retrieve the properties for all the folders in the file system, allowing you to easily map the layout of your disk drives. Although you probably do not need to enumerate all the folders on a regular basis, carrying out this task on occasion can help you spot anomalies in the file system, including such things as folders that:

  • Should never have been created.
  • Are no longer required.
  • Do not adhere to your file system naming convention.

To retrieve a list of all the folders on a computer, use the following WQL query:

" SELECT * FROM Win32_Directory" 

If you want to limit data retrieval to a single disk drive, include a Where clause specifying the drive letter. For example, this query returns a list of all the folders on drive C:

" SELECT * FROM Win32_Directory WHERE Drive = 'C'" 

If you need to enumerate all the folders on a computer, be aware that this query can take an extended time to complete. For example, on a Windows 2000 based computer with 5,788 folders, a script that returns the name of each folder required 429 seconds to complete.

Scripting Steps

Listing 11.2 contains a script that returns a list of all of the folders on a computer. 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 to query the Win32_Directory class.

    This returns a collection of all the folders on the computer.

  4. For each folder in the collection, echo the folder name.

Listing 11.2   Enumerating All the Folders on a Computer

1 2 3 4 5 6 7 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colFolders = objWMIService.ExecQuery("SELECT * FROM Win32_Directory") For Each objFolder in colFolders     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