Friday, January 14, 2011

Creating and using a variable array in DOS

http://www.sprint.net.au/~terbut/usefulbox/msdoscmds.htm

I searched everywhere without success for a method to produce an array in DOS.

This solution has proven to be very successful.

The basics is to use a variable name that has some type of separator, like a period ".", the second half of the variable name can then be substituted with the contents of another variable, following is very a simple example.

:: set the segment variables
set agbtp.1=A
set agbtp.2=B
Notice the period between the 'agbtp' and '1' ?

Next setup or supply a third variable that will be substituted into the array.

:: set the segid
set segid=1
Now put the whole lot in to a FOR-DO to obtain the new variable from the array based on the supplied segid. Note the '^=' in the delims, this allows the search to use the '=' as a separator. The 'find' with the ".variable=" is also necessary to do the correct filtering.

:: calculate the Segment printed variable
for /F "tokens=2 delims=^=" %%i in ('set agbtp.%segid% ^| find ".%segid%=" ') do set psegid=%%i
This resulted in the variable 'psegid' now containing 'A'.

If the variable 'segid' had been loaded with the value '2', then the 'psegid' would then contain 'B'.

This may all appear very simplistic, but if there was a problem where you wanted to count from 0 to 255, and on each count, produce the output in HEX (00-FF). Setup an array of:

set myhex.0=00
set myhex.1=01
set myhex.2=02
" " " - (repeat from 3 to 252 / 03 to FC)
set myhex.253=FD
set myhex.254=FE
set myhex.255=FF

Set up a counting loop.

set /A cntr=0
set /A scntr=256
:loop
Then substitute in the variable and output the results.

for /F "tokens=2 delims=^=" %%i in ('set myhex.%cntr% ^| find ".%cntr%=" ') do echo %%i
And loop until finished.

set /A cntr=%cntr%+1
if NOT [%scntr%]==[%cntr%] goto loop
This technique can be used to read data or configuration from a text file, into an array.

No comments:

Post a Comment