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

sas_fil_06c

Scripting Steps

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:

  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 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.

  4. 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


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
 ("SELECT * FROM CIM_Datafile WHERE Name = 'c:\\Scripts\\Adsi.vbs'")
For Each objFile in colFiles
 Wscript.Echo "Access mask: " & objFile.AccessMask
 Wscript.Echo "Archive: " & objFile.Archive
 Wscript.Echo "Compressed: " & objFile.Compressed
 Wscript.Echo "Compression method: " & objFile.CompressionMethod
 Wscript.Echo "Creation date: " & objFile.CreationDate
 Wscript.Echo "Computer system name: " & objFile.CSName
 Wscript.Echo "Drive: " & objFile.Drive
 Wscript.Echo "8.3 file name: " & objFile.EightDotThreeFileName
 Wscript.Echo "Encrypted: " & objFile.Encrypted
 Wscript.Echo "Encryption method: " & objFile.EncryptionMethod
 Wscript.Echo "Extension: " & objFile.Extension
 Wscript.Echo "File name: " & objFile.FileName
 Wscript.Echo "File size: " & objFile.FileSize
 Wscript.Echo "File type: " & objFile.FileType
 Wscript.Echo "File system name: " & objFile.FSName
 Wscript.Echo "Hidden: " & objFile.Hidden
 Wscript.Echo "Last accessed: " & objFile.LastAccessed
 Wscript.Echo "Last modified: " & objFile.LastModified
 Wscript.Echo "Manufacturer: " & objFile.Manufacturer
 Wscript.Echo "Name: " & objFile.Name
 Wscript.Echo "Path: " & objFile.Path
 Wscript.Echo "Readable: " & objFile.Readable
 Wscript.Echo "System: " & objFile.System
 Wscript.Echo "Version: " & objFile.Version
 Wscript.Echo "Writeable: " & objFile.Writeable
Next

Show: