Reputation: 11
I have a problem reading parameters that have been added to a subroutine. These parameters can of course be read in the following way.
call :test one two three
:test
echo %1
echo %2
echo %3
exit /b
But I would like to read it in a loop as long as they are filled.
I made the following for that, but I don't get it working properly. With my limited understanding of all this. I would normally request a variable on the 3 dots. So %p%.
call :test one two three
:test
set /a p=1
:while
if [%...] NEQ [] (
echo %...
set /a p=p+1
goto :while
)
exit /b
Can someone tell me how I solve this?
Upvotes: 0
Views: 69
Reputation: 11
Thanks @Stephan. It works just like i want :
:test
for %%A in (%*) do (
echo %%A
)
exit /b
Upvotes: 1
Reputation: 80203
@ECHO OFF
SETLOCAL
call :test one two three
call :test four five
call :test seven eight nine ten
CALL :test
goto :eof
:test
rem for /f "delims==" %%a in ('set parm 2^>nul') do set "%%a="
set /a parmcnt=0
:testlp
set /a parmcnt+=1
SET "parm%parmcnt%=%1"
SHIFT
IF DEFINED parm%parmcnt% GOTO testlp
SET /a parmcnt-=1
ECHO %parmcnt% parameters
IF %parmcnt% gtr 0 FOR /L %%a IN (1,1,%parmcnt%) DO CALL ECHO %%parm%%a%%
ECHO -------------------
SET parm
ECHO ===================
GOTO :EOF
The meat here is the :testlp
loop. The parameter count parmcnt
is incremented, and the variable parm...
is set to %1
- the first parameter, then the paramter list is SHIFT
ed which simply removes the first parameter from the apparent list.
if the parm...
has a value, then try again, until all of the parameters have been assigned to parm*
decrement the parameter count (since the last assignment attempt assigned a value of nothing as %1
did not exist) and display the results.
The result-display is in two parts. The first uses a parsing trick (to avoid delayed expansion
which is a whole new ball-game). The parser first substitutes for the metavariable %%a
, so what is executed is call echo %%parm1%%
etc. for 1
to %parmcnt%
. call
ing the echo
executes echo %parm1%
in a subshell.
The second simply shows all variables set
which start parm
. Note that parm%parmcnt+1% will always be empty, but there may be stray parm
s set from prior invocations of :test
Hence the remmed-out for ... set parm
line which clears all variables starting parm
. Removing the rem
from this line would dispose of the stray variables problem, but note that it would also clear any variablename that starts parm
- like parmlimit
had parmlimit
been set.
Upvotes: 0