1

I'm currently attempting to concatenate my command line arguments with this:

for %%a in (%*) do set "subject=%subject% %%a"

So for example if I run

my.bat subject line here

it should set my subject variable to "subject line here", preserving the spaces. However, currently after the run, my subject variable is set to the last word. I get a subject value of " here."

How to concatenate the command line arguments right?

1
  • In case of you want to know why environment variable subject does not have the expected string after the FOR loop, open a command prompt window, run set /? and read all output help pages. There is a for example very similar to yours on which is explained why delayed expansion must be used to concatenate the strings correct. Commented Aug 20, 2016 at 11:31

1 Answer 1

7

Can't you just do:

SET subject=%*

Alternatively enable delayed expansion so that the environment variables don't get substituted during parsing.

Setlocal EnableDelayedExpansion
for %%a in (%*) do set subject=!subject! %%a
echo %subject%

see Difference between %variable% and !variable! in batch file for more info.

Sign up to request clarification or add additional context in comments.

1 Comment

No Problem, if this solved your issue then please tick the solution as accepted.

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.