Enumerating Files and File Properties
Microsoft® Windows® 2000 Scripting Guide
In the NTFS file system, files are actually made up of two distinct parts. In addition to the file itself, such as the Microsoft Word document MyDocument.doc, NTFS files also contain "metadata" regarding the file. The file MyDocument.doc contains more than just the words typed into the document; MyDocument.doc also contains information about itself, including such things as file size, the date it was created, and whether it can be modified or is a read-only file.
The WMI CIM_Datafile class allows you to retrieve this metadata for any file on a computer. Using WMI, you can retrieve any of the file properties listed in Table 11.1 (file properties are the same as folder properties). To retrieve this information, simply bind to a file and then echo the appropriate properties.
The relationship between the CIM_DataFile class and Windows Explorer is shown in Figure 11.6.
Figure 11.6 CIM_DataFile and Windows Explorer
Listing 11.17 contains a script that returns the properties of the file C:\Scripts\Adsi.vbs. 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 CIM_Datafile class.
To limit data retrieval to a specific file, a Where clause is included restricting the returned files to those with the name C:\\Scripts\\Adsi.vbs. You must include both backslashes (\\) in the name.
For the single file in the collection, echo a number of the properties shown in Table 11.1.
Listing 11.17 Retrieving File Properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32