Batch-Script - Iterate through arguments

55,244

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.

Share:
55,244
Toby
Author by

Toby

Updated on July 09, 2022

Comments

  • Toby
    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 or counter) 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 variable counter. Meaning for counter=2 it gives me 2 and not the content of %2.

  • Toby
    Toby over 10 years
    Ok 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
    npocmaka over 10 years
    with call set "_var=%%!counter!".you need a temp variable because if cannot be called
  • npocmaka
    npocmaka over 10 years
    and then you can compare with if -> if "!_var!" equ "something" echo this is something
  • Toby
    Toby over 10 years
    Are you sure that I have to put the " around the whole set call?
  • npocmaka
    npocmaka over 10 years
    it 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
    Toby over 10 years
    The problem is, when I assign it to _var , _var also just contains %1 etc (using echo !_var!) - When I use call echo !_var! I get the contents again - the problem is when I use _var in my if call (liek you suggest) it unfortunately doesn't work
  • npocmaka
    npocmaka over 10 years
    call set ...the call accidentally was left outside the grey bckg.
  • npocmaka
    npocmaka over 10 years
    call set "_var=%%!counter!"
  • Jay Taylor
    Jay Taylor almost 8 years
    Where are the better answers? I like this one because it is super succinct, minimal, and straightforward.
  • Christian Rondeau
    Christian Rondeau over 7 years
    What 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
    isapir almost 6 years
    Is this approach unwise? And if so, why?
  • user1767316
    user1767316 over 5 years
    Tthis one is working as is, for me, unlike current solution from @npocmaka (windows 10 familly)
  • Arnaud
    Arnaud over 4 years
    @isapir This approach will fail if one of the argument contains * as I just learned the hard way.
  • Arnaud
    Arnaud over 4 years
    This answer works when the arguments contain special characters like * but iterates one time too many when there are no arguments.
  • M463
    M463 over 4 years
    A 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
    Jay Taylor over 4 years
    Very helpful information @Arnaud, thank you for explaining the issue and shedding light on this!
  • Caverna
    Caverna over 3 years
    that's my choice, for sure!
  • Cadoiz
    Cadoiz almost 3 years
    This has trouble when the argument has some format, e.g. this link: https://www.tagesschau.de/investigativ/ndr-wdr/spaeh-softwar‌​e-pegasus-projekt-10‌​1.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
    Cadoiz almost 3 years
    Found this to be by far the most reliable solution. It also handles parameter_iteration_3.bat https://www.tagesschau.de/investigativ/ndr-wdr/spaeh-softwar‌​e-pegasus-smartphone‌​-101.html?utm_source‌​=pocket-newtab-globa‌​l-de-DE ** * te*st as the only solution and also correctly deals with ""
  • Cadoiz
    Cadoiz almost 3 years
    Be 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
    Cadoiz almost 3 years
    Be aware that this needs the delayed expansion (setlocal enableDelayedExpansion), which my preferred answer doesn't.
  • npocmaka
    npocmaka almost 3 years
    @Cadoiz - mind that using labels and GOTO for loops can be low performing approach.
  • johny why
    johny why over 2 years
    this one is more understandable than @JayTaylor's answer. stackoverflow.com/a/39776524/209942
  • johny why
    johny why over 2 years
    Are you counting the number of command-line arguments?
  • Magoo
    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 in nparms is actually the last-detected parameter number that was defined. Change the loop to for %%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) as nparms