2

I have a batch file test.bat I understand we can give it multiple arguments and take those values using %1, %2, and so on. But I do not know how many arguments will be given. I thought of constructing an array and for loop to decide. But the point I am failing is constructing the for loop.

what I did is:

set count=0
// for loop until we have arguments left
set list[%count%]=%var%
set /A count=count+1

I know little bit of for loop in batch files, but I do not quite understand how to use in this scenario. Number of arguments are unknown. Any direction would be appreciated?

2 Answers 2

3

%* references all arguments. You could use

for %%a in (%*) do echo %%a

or use shift which advances %1 to the next argument and so on.

:loop
echo %1
shift
if not "%~1" == "" goto :loop
Sign up to request clarification or add additional context in comments.

Comments

0

You need to add double quotes around %1 to get the whole argument if it is a string.

@echo off
:loop
echo %1
shift
if not "%~1" == "" goto :loop

When you run it, you will see the result like this:

D:\2\test2>arg1 abc "abc def"
abc
"abc def"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.