Inside a batch file, how do you iterate through a subset of the command line arguments passed to the batch file?
I can find lots of examples, but none seems to deal with this exact scenario.
Inside a batch file, how do you iterate through a subset of the command line arguments passed to the batch file?
I can find lots of examples, but none seems to deal with this exact scenario.
Here is a demo example using shift command as suggested by Scott C:
@echo off
set "Count=0"
:NextParameter
if "%~1" == "" goto Done
set /A Count+=1
echo Parameter %Count% is: "%~1"
shift
goto NextParameter
:Done
echo %Count% parameters processed.
set "Count="
The absolute minimum loop would be:
@echo off
:NextParameter
if "%~1" == "" goto :EOF
echo Processing parameter: "%~1"
shift
goto NextParameter
In the loop always first parameter is evaluated for being an empty string. The loop exits if this condition becomes true.
Otherwise it can be done whatever should be done with the current parameter using "%~1"
or just %1
or whatever is needed, see help output in a command prompt window on executing there call /?
or help call
.
Command shift shifts all arguments to left by 1 making it possible to access the next parameter again with %1
. Help of command shift can be read with shift /?
executed in a command prompt window.
The two examples above fail to process all parameters correct if the batch file is called with 1 or more empty parameters.
For example calling minimum example with Arg1 "argument 2" "" "arg 4"
results in the output:
Processing parameter: "Arg1"
Processing parameter: "argument 2"
This can be solved with following batch code:
@echo off
:NextParameter
if "%~1" == "" (
if not [%1] == [""] goto :EOF
)
echo Processing parameter: "%~1"
shift
goto NextParameter
This third batch file called with Arg1 "argument 2" "" "arg 4"
outputs correct:
Processing parameter: "Arg1"
Processing parameter: "argument 2"
Processing parameter: ""
Processing parameter: "arg 4"
It is of course also possible to skip empty parameter in list, for example with:
@echo off
:NextParameter
if "%~1" == "" (
if [%1] == [""] (
echo Skipping empty parameter.
shift
goto NextParameter
)
goto :EOF
)
echo Processing parameter: "%~1"
shift
goto NextParameter
This last batch file called with Arg1 "argument 2" "" "arg 4"
outputs:
Processing parameter: "Arg1"
Processing parameter: "argument 2"
Skipping empty parameter.
Processing parameter: "arg 4"
But batch files are usually not called with empty parameters.
I think the simplest way is to first load the parameters in an array, and then process array elements in any way you wish:
@echo off
setlocal EnableDelayedExpansion
rem Load parameters in "parV" array and "parC" counter
set parC=0
for %%a in (%*) do (
set /A parC+=1
set parV[!parC!]=%%a
)
rem Show parameters from 3 to N
for /L %%i in (3,1,%parC%) do echo %%i- !parV[%%i]!
If the parameters may contain wild-card characters (*
or ?
), then the parameters must be loaded in the array via the classical shift
loop:
rem Load parameters in parV/parC even if they have wild-cards
set parC=0
:nextPar
if "%~1" equ "" goto endPars
set /A parC+=1
set "parV[!parC!]=%~1"
shift
goto nextPar
:endPars
For a further description on array management in Batch files, see this post.