Removing Key-Item Pairs from a Dictionary

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Key-Item pairs can also be removed from a Dictionary. The Script Runtime library provides two methods for removing key-item pairs from a Dictionary:

  • RemoveAll, which removes all the key-item pairs in a Dictionary.
  • Remove, which removes a specified key-item pair from the Dictionary.

Removing All Key-Item Pairs from a Dictionary

You might have scripts, particularly monitoring scripts, that perform some action, store the information in a Dictionary, and then either display the data or save the data to a text file or database. At that point, the script might pause for several minutes and then run a second time, gathering updated information. This pattern might continue indefinitely.

If you are using a Dictionary as a temporary storehouse for the gathered data, you will likely want to clear the Dictionary before you begin gathering the next set of data. To do that, you can simply use the RemoveAll method, which removes all the key-item pairs from the specified Dictionary.

For example, the script in Listing 4.50 creates a Dictionary with three elements. After displaying the Dictionary keys, the script deletes each key-item pair in the Dictionary by using the RemoveAll method in the following code statement:

objDictionary.RemoveAll 

To verify that the elements were removed, the script then echoes the Dictionary keys again.

Listing 4.50   Removing All the Key-Item Pairs in a Dictionary

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
Set objDictionary = CreateObject("Scripting.Dictionary") objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline" objDictionary.Add "Printer 3", "Printing" colKeys = objDictionary.Keys Wscript.Echo "First run: " For Each strKey in colKeys     Wscript.Echo strKey Next objDictionary.RemoveAll colKeys = objDictionary.Keys Wscript.Echo VbCrLf & "Second run: " For Each strKey in colKeys     Wscript.Echo strKey Next

When this script is run under the WSH CScript host, the following output appears in the command window. As you can see, the second time through the dictionary is empty and contains no keys (this is not an error, because collections are allowed to contain 0 items):

First run: Printer 1 Printer 2 Printer 3 Second run: 

Removing a Specific Key-Item Pair from a Dictionary

Instead of removing all the key-item pairs from a Dictionary, you might at times want to remove only a single key and item. For example, suppose you have a script that retrieves event-log events from a series of computers. The script is designed to try each computer in succession and to keep track of successes and failures. For each failed attempt to contact a computer, the script tries again, continuing on until all the computers have been contacted and all the event-log events retrieved.

In this scenario, you might have a Dictionary consisting of the following keys:

atl-dc-01 atl-dc-02 atl-dc-03 atl-dc-04 atl-dc-05 

When the script runs, it might be unable to contact computers atl-dc-03 and atl-dc-04. In that case, the script must attempt to contact these computers again. However, how will the script know which computers have been contacted and which ones have not?

One way to solve this problem is to simply remove successful contacts from the Dictionary; after the first iteration, the Dictionary will then consist only of the following elements.

atl-dc-03 atl-dc-04 

Each time a computer is contacted, the corresponding Dictionary element is removed. When the Dictionary no longer contains any elements, that means that all the computers have been contacted and the script can then complete.

To remove a Dictionary element, use the Remove method, passing the key name as the only parameter. For example, the following code statement removes the key atl-dc-02 (and its associated item):

objDictionary.Remove("atl-dc-02") 

The script in Listing 4.51 creates a Dictionary with three elements and then echoes the Dictionary keys. The script then removes the Printer 2 key and echoes the Dictionary keys again.

Listing 4.51   Removing a Specified Key-Item Pair from a Dictionary

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 
Set objDictionary = CreateObject("Scripting.Dictionary") objDictionary.Add "Printer 1", "Printing" objDictionary.Add "Printer 2", "Offline" objDictionary.Add "Printer 3", "Printing" colKeys = objDictionary.Keys Wscript.Echo "First run: " For Each strKey in colKeys     Wscript.Echo strKey Next objDictionary.Remove("Printer 2") colKeys = objDictionary.Keys Wscript.Echo VbCrLf & "Second run: " For Each strKey in colKeys     Wscript.Echo strKey Next

When this script is run under CScript, the following output appears in the command window:

First run: Printer 1 Printer 2 Printer 3 Second run: Printer 1 Printer 3 

At the end of the second iteration, the key Printer 2 is no longer present in the Dictionary.


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