Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: CreateDirectoryTree() aka CreateFolderTree - Native VBSCRIPT can not do this[Next]: GetEnv() - Get Environment Variables
\->VBSCRIPT Files->Dictionary Objects - store and retrieve information under a key

[anchor]

The Code for: "Dictionary Objects [store and retrieve information under a key].vbs"

This is the example, a shortcut was installed to this code :

'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
' $Header:   C:/DBAREIS/Projects.PVCS/Win32/ScriptingTipsAndTricks/EXAMPLE[vbs].GetEnv() [Get Environment Variables].vbs.txt.pvcs   1.0   11 Jul 2014 19:31:06   USER "Dennis"  $
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

const AllowDuplicates = false
const SortDictByKey   = 1
const SortDictByValue = 2
dim oMemory : set oMemory = CreateObject("Scripting.Dictionary")

'--- Add to the dictionary object -------------------------------------------
say "Adding items to the dictionary object"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
TEST_AddToMemory "ZZ", "Value ZZ"
TEST_AddToMemory "ZZ", "Value ZZ"           'Already exists
TEST_AddToMemory "AA", "Value AA"
TEST_AddToMemory "aa", "Value aa"           'Different case so will be added
TEST_AddToMemory "CC", "Value CC"
TEST_AddToMemory "Aa", "Value Aa"


'--- Query the dictionary object --------------------------------------------
say ""
say "Query items in the dictionary object"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
TEST_QueryMemory "ZZ"
TEST_QueryMemory "BB"
TEST_QueryMemory "Aa"

'--- Get a list of all the keys ---------------------------------------------
say ""
say "Listing Keys"
say "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
say "In Added Order    : " & TestGetListOfKeys()
DictionarySortAsStrings oMemory, SortDictByKey, false
say "Sorted by KEY (CI): " & TestGetListOfKeys()
DictionarySortAsStrings oMemory, SortDictByKey, true
say "Sorted by KEY (CS): " & TestGetListOfKeys()


'============================================================================
sub TEST_AddToMemory(Key, KeyValue)
'============================================================================
    say "ADDING( " & Key & " ) = """ & KeyValue & """"
    say "   * Added: " & AddToMemory(Key, KeyValue)
end sub

'============================================================================
sub TEST_QueryMemory(Key)
'============================================================================
    say "Querying( " & Key & " )"
    if  oMemory.Exists(Key) then
        say "   = """ & oMemory(Key) & """"
    else
        say "   * ### KEY DOES NOT EXIST ###"
    end if
end sub


'============================================================================
sub TEST_SortMemory(Key)
'============================================================================
    say "Querying( " & Key & " )"
    if  oMemory.Exists(Key) then
        say "   = """ & oMemory(Key) & """"
    else
        say "   * ### KEY DOES NOT EXIST ###"
    end if
end sub


'============================================================================
function TestGetListOfKeys()
'============================================================================
    TestGetListOfKeys  = ""
    dim oAllKeys : oAllKeys = oMemory.Keys
    dim i
    for i = 0 to oMemory.Count-1
        '--- Handle one item in the array ---------------------------------------
        if i <> 0 then
           TestGetListOfKeys = TestGetListOfKeys & ", "
        end if
        TestGetListOfKeys = TestGetListOfKeys & oAllKeys(i)
    next
end function




'============================================================================
function AddToMemory(Key, KeyValue)
'============================================================================
    '--- Does the "key" already exist in the dictionary object? -------------
    AddToMemory = true
    if  not oMemory.Exists(Key) then
        '--- It doesn't so add it -------------------------------------------
        oMemory.add Key, KeyValue
    else
        '--- Key exists! What do we want to do? -----------------------------
        if  not AllowDuplicates then
            AddToMemory = false
        else
            '--- Append to the previous value -------------------------------
            dim NewValue : NewValue = oMemory(Key)   'Get current value
            NewValue = NewValue & ", " & KeyValue    'Append the new text
            oMemory.Remove Key                       'Have to remove the key before we can add it again
            oMemory.add Key, NewValue                'Store the combined value :-)
        end if
    end if
end function


'============================================================================
function DictionarySortAsStrings(oDictionary, SortBy, CaseSensitive)
' Improved version of: http://support.microsoft.com/kb/246067
'============================================================================
    '--- Won't bother to sort if zero or one item ---------------------------
    dim strDict()
    dim objKey
    dim strKey,strItem
    dim X,Y,Z
    Z = oDictionary.Count
    if  Z > 1 Then
        '--- OK, more than one items so we will sort, make sure everything is a string ---
        redim strDict(Z,2)
        X = 0
        for each objKey In oDictionary
            strDict(X,SortDictByKey)   = cstr(objKey)
            strDict(X,SortDictByValue) = cstr(oDictionary(objKey))
            X = X + 1
        next

        '--- Case sensitive sort? -------------------------------------------
        dim SortHow
        if  CaseSensitive then
            SortHow = vbBinaryCompare
        else
            SortHow = vbTextCompare
        end if

        '--- Perform the sort -----------------------------------------------
        for X = 0 to (Z - 2)
              for Y = X to (Z - 1)
                    if StrComp(strDict(X,SortBy), strDict(Y,SortBy), SortHow) > 0 then
                        strKey                     = strDict(X,SortDictByKey)
                        strItem                    = strDict(X,SortDictByValue)
                        strDict(X,SortDictByKey)   = strDict(Y,SortDictByKey)
                        strDict(X,SortDictByValue) = strDict(Y,SortDictByValue)
                        strDict(Y,SortDictByKey)   = strKey
                        strDict(Y,SortDictByValue) = strItem
                    end if
              next
        next

        '--- Add the items back to the dictionary in order ------------------
        oDictionary.RemoveAll
        for X = 0 to (Z - 1)
          oDictionary.Add strDict(X,SortDictByKey), strDict(X,SortDictByValue)
        next
    end if
end function



'============================================================================
sub Say(What)
'============================================================================
    wscript.echo What
end sub


Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.Please email me any feedback, additional information or corrections.
See this page online (look for updates)

[Top][Contents][Prev]: CreateDirectoryTree() aka CreateFolderTree - Native VBSCRIPT can not do this[Next]: GetEnv() - Get Environment Variables


ScriptingTipsAndTricks© is (C)opyright Dennis Bareis 2003-2008 (All rights reserved).
Sunday September 07 2014 at 12:50pm
Visit ScriptingTipsAndTricks's Home Page
Microsoft awarded me an MVP (Most Valuable Professional award) in 2004, 2005, 2006, 2007, 2008 & 2009 for the Windows SDK (Windows Installer) area.