Verifying the Existence of a Specific Key

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

One of the major advantages the Dictionary object has over an array or a standard collection is that you can quickly verify the existence of a particular key. For example, suppose you have a list of files installed on a computer and want to search that list to ensure that a particular set of DLLs has been installed. With either a collection or an array, there is no way to verify the existence of any one file without methodically looping through the entire set and checking each item individually.

With a Dictionary, however, you can use the Exists method to verify the existence of any given key. The Exists method takes a single parameter (the name of the key) and then returns a Boolean value: True if the key is in the Dictionary, False if it is not.

For example, the script in Listing 4.48 creates a Dictionary object and adds three elements: Printer 1, Printer 2, and Printer 3. The script then checks for the existence of a key named Printer 4 and echoes back the results of that check.

Listing 4.48   Verifying the Existence of a Dictionary Key

1 2 3 4 5 6 7 8 9 
Set objDictionary = CreateObject("Scripting.Dictionary") objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline" objDictionary.Add "Printer 3", "Printing" If objDictionary.Exists("Printer 4") Then     Wscript.Echo "Printer 4 is in the Dictionary." Else     Wscript.Echo "Printer 4 is not in the Dictionary." End If

When this script runs, the message "Printer 4 is not in the Dictionary" is echoed to the screen.


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