@ECHO Off
SETLOCAL ENABLEDELAYEDEXPANSION
:: remove variables starting $ or #
For %%b IN ($ #) DO FOR /F "delims==" %%a In ('set %%b 2^>Nul') DO SET "%%a="
SET /a count=0
SET "select=this is select's original value"
ECHO parameters received = %*
:loop
REM SET $... and #... to the parameter-pair.
SET $%count%=%1
SET #%count%=%2
IF NOT DEFINED $%count% GOTO process
IF NOT DEFINED #%count% ECHO "Incomplete pair"&GOTO :EOF
SET /a count +=1
:: shift twice to move 2 parameters
shift&shift&GOTO loop
:process
ECHO %count% pairs found
SET $
SET #
FOR /L %%a IN (1,1,%count%) DO (
REM note that this changes "select"
SET /a select=%%a - 1
CALL SET /a item1=%%$!select!%%
CALL SET /a item2=%%#!select!%%
ECHO pair %%a is !item1! and !item2! from $!select! and #!select! NOT $#%select%
)
ECHO ----------------- select = %select% ----------
SET /a count-=1
FOR /L %%a IN (0,1,%count%) DO (
REM note that this changes "select"
SET /a select=%%a + 1
ECHO pair !select! is !$%%a! and !#%%a! from $%%a and #%%a NOT $#%select%
GOTO :EOF
First, set up some initial values. The first for
ensures that any variables starting $
or #
are cleared.
Set $0
and #0
to the first and second parameters. If $0 is not set, then we have no further data, so process what we have. If $0 is set but #0 is not, we don't have a pair so complain and terminate.
If both #0 ans $0 are set, increment count
, shift
the parameters twice and run around the loop. The next pair will be placed in $/#1 and so on until the parameters are exhausted.
Now show the count of parameters and the $
and #
values that have been set.
Next loop show unnecessary manipulation of the variables.
- First set
select
to 1 less than the loop-counter.
- then use a parsing trick to get the value of $[current value of select]. The line translates to "SET /a item1=%$n%" where
n
is the run-time value of select
- ie. the value as it changes through the loop.
- then show the results.
item1
is changed within the block (parenthesised series of statements) so we need !item1
to access that changed value - %item1%
would give us the value as it was when the loop was parsed
. You can see that from the report for select
on the same line.
Last loop is the same idea - but varying %%a from 0 to (count-1). Observe that select
in the dashed echo
line has the last value it was assigned in the previous loop.
So, once again we change select
within the block, so we see a difference between !select!
and %select%
. We can also access $0
directly using !$%%a!
- that is the current value of the variable [$
strung with the current value of the loop-control %%a
]