Reputation: 13
I am trying to run more than one command in for /f in the windows command line (or from a batch file). Here's simplified code that exhibits the problem:
for /f "usebackq tokens=*" %a IN (`echo a & echo b`) do echo %a
This gives the error message:
& was unexpected at this time.
Each of the following works fine:
for /f "usebackq tokens=*" %a IN (`echo a`) do echo %a
for /f "usebackq tokens=*" %a IN (`echo b`) do echo %a
echo a & echo b
So my question is whether or not it is possible to run two commands together the way I am trying to do in a single for /f loop.
Thanks for any help you can provide.
Upvotes: 1
Views: 842
Reputation: 9266
You need to escape the &
so it's parsed as part of the subexpression, and not as part of the FOR. Change &
to ^&
for /f "usebackq tokens=*" %a IN (`echo a ^& echo b`) do echo %a
As a side note, this line will work at the command line, but usually you intend to put it into a batch file. In the batch file, you will need to double the percent signs as well by changing %a
to %%a
, like this:
for /f "usebackq tokens=*" %%a IN (`echo a ^& echo b`) do echo %%a
Upvotes: 3