0

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?

Mofi
  • 46,139
  • 17
  • 80
  • 143
thuy
  • 13
  • 1
  • 4
  • 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. – Mofi Aug 20 '16 at 11:31

1 Answers1

4

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.

Community
  • 1
  • 1
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41