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
.
For example:
Another one:
For simple iteration can't we just check for additional arguments with "shift /1" at the end of the code and loop back? This will handle more than 10 arguments, upper limit not tested.
here's one way to access the second (e.g.) argument (this can be put in for /l loop):
so:
This should show how to extract random parameters by position.
If to keep the code short rather than wise, then