Some questions: I want to process all commandline parameters in a loop. I.e. a call like
Code: Select all
call :foo a "" c
shall detect 3 parameters. So I cannot break at an empty parameter but on the first undefined parameter.
I want to get the number cnt of parameters, a variable p1, p2, etc for each parameter and a collected string ps like "/p1/p2/p3#" for all parameters.
Without a loop one would write codepieces in sequence:
Code: Select all
...
set c1=%1
if not defined c1 goto Done
set /a cnt=3
set p3=%~1
set ps=%ps%/%~1
shift
...
Q1a: Is it true that there is no way to get the number of parameters directly? You must test each parameter individually?
Q1b: And you cannot test directly if %1 is defined? You must use:
Code: Select all
set c1=%1
if not defined c1 goto Done
I needed for a while to get that into a loop. Now it works:
Code: Select all
setlocal enabledelayedexpansion
set /a cnt=0
set ps=
for /l %%i in (1,1,20) do (
call set "c1=%%1"
if not defined c1 goto Done
call set "c1h=%%~1"
set /a cnt=%%i
set p%%i=!c1h!
set ps=!ps!/!c1h!
shift
)
:Done
set ps=%ps%#
echo We have %cnt% parameters: %ps%
echo Namely p1=%p1%, p2=%p2%, p3=%p3%, etc.
echo All in a loop:
for /l %%i in (1,1,%cnt%) do (
echo #%%i: p%%i=!p%%i!
)
With output for call :foo a "" c:
Code: Select all
We have 3 parameters: /a//c#
Namely p1=a, p2=, p3=c, etc.
All in a loop:
#1: p1=a
#2: p2=
#3: p3=c
Q2: I dont like that fixed upper bound 20 in the loop. The body is executed only 3 times but under "echo on" one sees 20 expansions of the body. And why 20, not 47 ? Can I write a simple "do while"-type of a loop without an upper bound?
Q3: I was not able to get rid of that helper variable c1h which I need only for the %~1 expansion. Is there a way to use %1, %~1 directly within the loop body?
Thanks for help.