Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
The script for enumerating all the files on a computer (as shown in Listing 11.19) works as long as there is sufficient memory to maintain a list of all the files on a computer. For example, on a Windows 2000 based computer with 128 MB of RAM and approximately 22,000 files, the enumeration script completed successfully. On a similar computer with approximately 82,000 files, however, the script exhausted available memory and failed.
With a standard query, data retrieval can tie up available memory and slow system performance. If you expect your file query to return a large amount of data, you might find it more expedient to use an asynchronous query, even if you are returning only a subset of files on a computer (for example, a query that returns several thousand .doc files from a file server). With an asynchronous query, objects are retrieved and reported one at a time. This allows the script to proceed with other tasks; equally important, it allows data retrieval to take place in the background, lessening the system load.
Scripting Steps
Listing 11.20 contains a script that uses an asynchronous query to return a list of all the files on a computer. To carry out this task, the script must perform the following steps:
- Create a constant named POPOUP_DURATION, and set the value to 120. This ensures that the popup message box remains on screen for 120 seconds, allowing enough time for the asynchronous query to begin returning data.
- Create a constant named OK_BUTTON and set the value to 0. This constant is used in constructing the popup message box.
- Create an instance of the Wscript Shell object.
- 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."
- Create an SWbemSink object named SINK_.
- Use the ExecQueryAsync method to tie the SWbemSink object to the WQL query "SELECT * FROM CIM_Datafile". This query returns a list of all the files on the computer and sends that list, one file at a time, to the SWbemSink object.
- As each file is returned to the SWbemSinkObject, display the file name.
Listing 11.20 Using an Asynchronous Query to Enumerate All the Files on a Computer
1 2 3 4 5 6 7 8 9 10 11 12 13
| Const POPUP_DURATION = 120 Const OK_BUTTON = 0 Set objWSHShell = Wscript.CreateObject("Wscript.Shell") strComputer = "." Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set objSink = WScript.CreateObject("WbemScripting.SWbemSink","SINK_") objWMIService.ExecQueryAsync objSink, "SELECT * FROM CIM_DataFile" objPopup = objWshShell.Popup("Starting event retrieval", _ POPUP_DURATION, "Event Retrieval", OK_BUTTON) Sub SINK_OnObjectReady(objEvent, objAsyncContext) Wscript.Echo objEvent.Name End Sub |
Send us your feedback | « Previous | Next » |