Enumerating All the Files in a Folder

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Although it is possible to enumerate all the files on the computer, this procedure can be extremely slow, and typically yields far more information than you can make use of. A faster, and generally more useful, approach is to enumerate all the files in a particular folder. This allows you to do such things as verify that each of your servers has a Scripts folder containing a full set of system administration scripts. Likewise, you might want to connect to an application folder and verify the version number for each .dll file stored there.

To enumerate all the files in a folder, you can use a script similar to the one shown in Listing 11.19. The only difference is that you include a Where clause in your WQL query to restrict data retrieval to those files found in the specified folder.

Scripting Steps

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

    To limit data retrieval to the files in a specific folder, a Where clause is included restricting the returned files to those with the path \\Scripts\\. (You must include double backslashes [\\] before and after the path name.) Note that you do not have to include the drive letter provided the path is unique within the file system.

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

Listing 11.21   Enumerating All the Files in a Folder

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 WHERE Path = '\\Scripts\\'") 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