Batch-Script - Iterate through arguments
Solution 1
here's one way to access the second (e.g.) argument (this can be put in a for /l
loop, see below.):
@echo off
setlocal enableDelayedExpansion
set /a counter=2
call echo %%!counter!
endlocal
so:
setlocal enableDelayedExpansion
set /a counter=0
for /l %%x in (1, 1, %argCount%) do (
set /a counter=!counter!+1
call echo %%!counter!
)
endlocal
Solution 2
@echo off
setlocal enabledelayedexpansion
set argCount=0
for %%x in (%*) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~x"
)
echo Number of processed arguments: %argCount%
for /L %%i in (1,1,%argCount%) do echo %%i- "!argVec[%%i]!"
For example:
C:> test One "This is | the & second one" Third
Number of processed arguments: 3
1- "One"
2- "This is | the & second one"
3- "Third"
Another one:
C:> test One Two Three Four Five Six Seven Eight Nine Ten Eleven Twelve etc...
Number of processed arguments: 13
1- "One"
2- "Two"
3- "Three"
4- "Four"
5- "Five"
6- "Six"
7- "Seven"
8- "Eight"
9- "Nine"
10- "Ten"
11- "Eleven"
12- "Twelve"
13- "etc..."
Solution 3
:loop
@echo %1
shift
if not "%~1"=="" goto loop
Solution 4
If to keep the code short rather than wise, then
for %%x in (%*) do (
echo Hey %%~x
)
Solution 5
@ECHO OFF
SETLOCAL
SET nparms=0
FOR /l %%i IN (1,1,20) DO (
SET myparm=%%i
CALL :setparm %*
IF DEFINED myparm SET nparms=%%i&CALL ECHO Parameter %%i=%%myparm%%
)
ECHO there were %nparms% parameters in %*
GOTO :EOF
:setparm
IF %myparm%==1 SET myparm=%1&GOTO :EOF
shift&SET /a myparm -=1&GOTO setparm
GOTO :eof
This should show how to extract random parameters by position.
Toby
Updated on July 09, 2022Comments
-
Toby almost 2 years
I have a batch-script with multiple arguments. I am reading the total count of them and then run a for loop like this:
@echo off setlocal enabledelayedexpansion set argCount=0 for %%x in (%*) do set /A argCount+=1 echo Number of processed arguments: %argCount% set /a counter=0 for /l %%x in (1, 1, %argCount%) do ( set /a counter=!counter!+1 )
What I want to do now, is to use my running variable (
x
orcounter
) to access the input arguments. I am thinking aobut something like this:REM Access to %1 echo %(!counter!)
In an ideal world this line should print out my first command line argument but obviously it doesn't. I know I am doing something wrong with the
%
operator, but is there anyway I could access my arguments like this?//edit: Just to make things clear - the problem is that
%(!counter!)
provides me with the value of the variablecounter
. Meaning forcounter=2
it gives me2
and not the content of%2
. -
Toby over 10 yearsOk I need to ask, even if it may obvious and I am just having a blackout: Is there any way how I can get that value into a variable now? Now that I have read the Input paramaeters I want to compare them with some strings. And I am not quite sure how I can access that string now...?
-
npocmaka over 10 yearswith call
set "_var=%%!counter!"
.you need a temp variable because if cannot be called -
npocmaka over 10 yearsand then you can compare with if ->
if "!_var!" equ "something" echo this is something
-
Toby over 10 yearsAre you sure that I have to put the
"
around the wholeset
call? -
npocmaka over 10 yearsit is
a safe
strategy. this allows you to include special characters in the value like|&><....
and will ensure you that there is no extra space at the end (which in some cases could break your logic) -
Toby over 10 yearsThe problem is, when I assign it to
_var
,_var
also just contains%1
etc (usingecho !_var!
) - When I usecall echo !_var!
I get the contents again - the problem is when I use_var
in myif
call (liek you suggest) it unfortunately doesn't work -
npocmaka over 10 years
call set
...the call accidentally was left outside the grey bckg. -
npocmaka over 10 years
call set "_var=%%!counter!"
-
Jay Taylor almost 8 yearsWhere are the better answers? I like this one because it is super succinct, minimal, and straightforward.
-
Christian Rondeau over 7 yearsWhat I think @jeb is trying to say is that you should provide an explanation for your answer. I think it's great, but with details on what each command does, and maybe a usage example, you'll get more upvotes :)
-
isapir almost 6 yearsIs this approach unwise? And if so, why?
-
user1767316 over 5 yearsTthis one is working as is, for me, unlike current solution from @npocmaka (windows 10 familly)
-
Arnaud over 4 years@isapir This approach will fail if one of the argument contains
*
as I just learned the hard way. -
Arnaud over 4 yearsThis answer works when the arguments contain special characters like
*
but iterates one time too many when there are no arguments. -
M463 over 4 yearsA little more context on how the whole thing works would be nice, but the solutions does what it says on the thin, I give you that. :-)
-
Jay Taylor over 4 yearsVery helpful information @Arnaud, thank you for explaining the issue and shedding light on this!
-
Caverna over 3 yearsthat's my choice, for sure!
-
Cadoiz almost 3 yearsThis has trouble when the argument has some format, e.g. this link:
https://www.tagesschau.de/investigativ/ndr-wdr/spaeh-software-pegasus-projekt-101.html
. I can't name the exact problem, but it doesn't even count this argument. It probably also has problems if one of the argument contains*
. -
Cadoiz almost 3 yearsFound this to be by far the most reliable solution. It also handles
parameter_iteration_3.bat https://www.tagesschau.de/investigativ/ndr-wdr/spaeh-software-pegasus-smartphone-101.html?utm_source=pocket-newtab-global-de-DE ** * te*st
as the only solution and also correctly deals with""
-
Cadoiz almost 3 yearsBe aware that this needs the delayed expansion (
setlocal enableDelayedExpansion
), which my preferred answer doesn't. You could also add an@echo off
in front. -
Cadoiz almost 3 yearsBe aware that this needs the delayed expansion (
setlocal enableDelayedExpansion
), which my preferred answer doesn't. -
npocmaka almost 3 years@Cadoiz - mind that using labels and GOTO for loops can be low performing approach.
-
johny why over 2 yearsthis one is more understandable than @JayTaylor's answer. stackoverflow.com/a/39776524/209942
-
johny why over 2 yearsAre you counting the number of command-line arguments?
-
Magoo over 2 years@johnywhy No, that was done in the original post. This demonstration is of how to access any arbitrary parameter by position since for instance %13 to get the 13th parameter will actually tag "3" onto the end of the first parameter (%1) as only a single digit is allowed in this position. The demo shows the first 20 parameters as set by the
for/l
loop. The number innparms
is actually the last-detected parameter number that was defined. Change the loop tofor %%i in (6,3,25,11,9) do
and it will report the last parameter number defined of the set(6,3,25,11,9)
asnparms