Share via

Enumerating Folders and Folder Properties

Microsoft® Windows® 2000 Scripting Guide

The primary advantage of using scripts for file system management is the fact that scripts can carry out tasks that would be too tedious and time-consuming to perform using either the graphical user interface or a command-line tool. For example, suppose you have 500 domain controllers, and a new organizational policy requires you to verify the following:

  • A folder named Scripts exists on each of these domain controllers.

  • The Scripts folder is hidden.

  • The Scripts folder is marked as read-only.

  • The Scripts folder is compressed.

It would take a significant amount of time to carry out this task using Windows Explorer or a command-line tool. By contrast, in about a half hour you can write a script that connects to each domain controller, performs the requisite checks, and logs the data. You can start this script before you go home, and when you return to work the next day, the verification will be complete.

Scripts can be used to carry out tasks such as these because, within the Windows shell, folders are actually COM objects. As COM objects, folders have properties that can be retrieved, properties that answer questions such as:

  • Is this folder hidden?

  • Is this folder read-only?

  • Is this folder compressed?

You can retrieve the properties of any folder in the file system using the Win32_Directory class. The properties available using this class are shown in Table 11.1. To retrieve the properties for a single folder, construct a Windows Query Language (WQL) query for the Win32_Directory class, making sure that you include the name of the folder. For example, this query binds to the folder D:\Archive:

" SELECT * FROM Win32_Directory WHERE Name = 'D:\\Archive'"

When specifying a file or folder name in a WQL query, be sure you use two backslashes (\\) to separate path components.

Scripting Steps

Listing 11.1 contains a script that retrieves properties for 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 to query the Win32_Directory class.

    To limit data retrieval to a specified folder, a Where clause is included restricting the returned folders to those where Name equals C:\\Scripts. You must include both backslashes (\\) in the specified name.

  4. For the single folder in the collection, echo the properties shown in Listing 11.1.

Listing 11.1 Retrieving Folder Properties

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFolders = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Directory WHERE Name = 'c:\\Scripts'")
For Each objFolder in colFolders
 Wscript.Echo "Archive: " & objFolder.Archive
 Wscript.Echo "Caption: " & objFolder.Caption
 Wscript.Echo "Compressed: " & objFolder.Compressed
 Wscript.Echo "Compression method: " & objFolder.CompressionMethod
 Wscript.Echo "Creation date: " & objFolder.CreationDate
 Wscript.Echo "Encrypted: " & objFolder.Encrypted
 Wscript.Echo "Encryption method: " & objFolder.EncryptionMethod
 Wscript.Echo "Hidden: " & objFolder.Hidden
 Wscript.Echo "In use count: " & objFolder.InUseCount
 Wscript.Echo "Last accessed: " & objFolder.LastAccessed
 Wscript.Echo "Last modified: " & objFolder.LastModified
 Wscript.Echo "Name: " & objFolder.Name
 Wscript.Echo "Path: " & objFolder.Path
 Wscript.Echo "Readable: " & objFolder.Readable
 Wscript.Echo "System: " & objFolder.System
 Wscript.Echo "Writeable: " & objFolder.Writeable