Hopefully "ScriptingTipsAndTricks" helps you with your batch file or vbscript scripting :-)
[Bottom][Contents][Prev]: RestartCscriptIn32BitModeIfRequired() - if in 64 bit mode[Next]: SortDictionary() - Sorts dictionary objects
\->VBSCRIPT Files->SortArray() - Sort single dimension array

SortArray() [Sort single dimension array].vbs

This example shows how you can pass an array to a function or subroutine and have the function update it (note that "ByRef" is used).

Note that a subroutine taking an array as a parameter must not be called using brackets as a VBSCRIPT bug prevents the updated array being passed back (it acts like it was passed "Byval"!

[anchor]

The Code for: "SortArray() [Sort single dimension array].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"  $
'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


'--- BEFORE -----------------------------------------------------------------
dim SomeText : SomeText = array("BBB", "AAA", "ZZZ", "GGG")
for i = 0 to ubound(SomeText)
    wscript.echo "UNSORTED: " & SomeText(i)
next
wscript.echo ""


    '--- SORT THE ARRAY. NOTE: VBSCRIPT FAILS IF: SortArray(SomeText) -------
    SortArray SomeText


'--- AFTER ------------------------------------------------------------------
wscript.echo ""
for i = 0 to ubound(SomeText)
    wscript.echo "SORTED  : " & SomeText(i)
next
wscript.quit 0


'============================================================================
sub SortArray(ByRef ArrayName)
'============================================================================
    on error resume next
    dim SortI, SortJ, SortTmp
    for SortI = 0 to ubound(ArrayName)
       for SortJ = SortI+1 to ubound(ArrayName)
           if  ArrayName(SortI) > ArrayName(SortJ) then
               SortTmp          = ArrayName(SortJ)
               ArrayName(SortJ) = ArrayName(SortI)
               ArrayName(SortI) = SortTmp
           end if
       next
    next
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]: RestartCscriptIn32BitModeIfRequired() - if in 64 bit mode[Next]: SortDictionary() - Sorts dictionary objects


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.