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.
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:
Create a variable to specify the computer name.
Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
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.
For the single folder in the collection, echo the properties shown in Listing 11.1.
Listing 11.1 Retrieving Folder Properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23