Enumerating All the Files on a Computer

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

The CIM_Datafile class provides access to all the files stored on a computer, including those stored on hard disks, floppy disks, and CD-ROM drives. This means that you can use WMI and the CIM_Datafile class to retrieve a list of all the files stored on a computer.

Admittedly, retrieving a list of all the files on a computer rarely has any practical value. Queries such as this can take an extremely long time to complete; on a large file server, for example, it might take several hours to return such a list; even then, the query will complete only if there is enough memory on the computer to carry out the request. This is due to the way standard WMI queries operate. As objects (such as files) are enumerated, they are queued in memory and then reported in batches. If too many objects are found, memory can exhausted before the entire list has been generated.

In addition, a query such as this typically returns far more information than is useful. After all, even a typical user workstation might contain some 50,000 files.

Although enumerating all the files on a computer might have little practical value, it does provide an introduction to the CIM_Datafile class and emphasizes the importance of using targeted queries when working with this class. Unlike many other WMI classes, some care must be taken when using CIM_Datafile. For example, when you return a list of services on a computer using WMI, a script completes in approximately the same time regardless of whether you return the entire set of services or some subset of services (for example, all the services currently running). This is not true of CIM_Datafile. Returning a list of all the files on a computer might take hours; returning a list of all the files with the .jpg extension might take just a few seconds.

Scripting Steps

Listing 11.19 contains a script that returns a list of all the files 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 CIM_Datafile class.

    This returns a collection of all the files on the computer.

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

Listing 11.19   Enumerating All the Files on a Computer

1 2 3 4 5 6 7 8 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colFiles = objWMIService.ExecQuery _     ("SELECT * FROM CIM_Datafile") 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