Reputation: 188
Please note: This question is not about how to echo without a newline. It's about to pipe a variable without a newline and store the result in another variable. Please don't mark this question as duplicate to other questions answering only how to remove newlines!
I have a variable a
which I want to pass to a program (let's use more
for the sake of this example), and its result should be stored into another variable b
. This is to prevent the creation of temporary files. Sticking some answers from other questions here together, this could be achieved by something like this:
FOR /f "tokens=*" %%t IN ('ECHO %a% ^| MORE') DO SET b=%%t
Which works -- BUT will add another newline on my variable! So I tried the following tricks you find on stackoverflow/superuser to prevent the newline. If you do these without the loop, they work perfectly! But once you put them in a loop, they fail:
FOR /f "tokens=*" %%t IN ('ECHO ^| SET /p="%a%" ^| MORE') DO SET b=%%t
FOR /f "tokens=*" %%t IN ('^<NUL SET /p="%a%" ^| MORE') DO SET b=%%t
It will always say The syntax of the command is incorrect.
What am I missing? Please help me!
Upvotes: 0
Views: 3562
Reputation: 70971
If you run your command
FOR /f "delims=" %%t IN ('^<NUL SET /p="%a%" ^| more ') DO SET "b=%%t"
with echo
ON you will see the equal sign in the set
command has vanished, it has been removed by the parser leaving an incorrect command.
You have two options:
FOR /f "delims=" %%t IN ('^<NUL SET /p^="%a%" ^| more ') DO SET "b=%%t"
FOR /f "delims=" %%t IN ('^<NUL SET /p"=%a%" ^| more ') DO SET "b=%%t"
The first one escapes the equal sign. The second one quotes it.
Upvotes: 5