Enumerating a Specific Set of Folders

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

The previous two topics generating a list of all the folders on a computer, and generating a list of all the subfolders contained in a specified folder illustrate basic forms of folder management. Unfortunately, real-world folder management does not always fall into such tidy categories. Instead, folder management often requires you to search through the entire file system looking for folders that meet specific criteria.

For example, you might need to return a list of all the encrypted folders or all the compressed folders on a computer, regardless of their physical location. With the FileSystemObject, this type of task can be achieved only by methodically retrieving each folder on each drive and then checking to see whether it meets the criteria.

WMI makes it much easier to return a list of folders meeting specific criteria; all you have to do is include the criteria within your WQL query. For example, this query returns a collection of all the compressed folders on a computer:

SELECT * FROM Win32_Directory WHERE Compressed = True 

Scripting Steps

Listing 11.4 contains a script that returns a list of all the hidden 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.

    To limit data retrieval to hidden folders, a Where clause is included restricting the returned folders to those for which the Hidden property is True.

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

Listing 11.4   Enumerating a Specific Set of Folders

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