Solved redirect issue with variables

Custom / CUSTOM
December 4, 2014 at 15:38:09
Specs: windows 7, 2.401 GHz / 2047 MB
I'm looking for an easy way to avoid the following problem:

Suppose I have a variable:

C:\>set var=1

And I redirect that variable to a file:

C:\>echo %var% >

That all works :


But, I don't want a trailing space in my file, so I glue the redirection to the content of the variable (here called "var"). From the above command, you see that in this example, that value is set to : 1

C:\>echo %var%>

And now, the result is that no file is written, because the command is parsed as:
C:\>echo 1>

And that indeed, results in :

ECHO is on.


But obviously, I want the variable's value in my file.
Obviously, if you put other content in the variable, there is no issue, but there is for specific values, like the one I used in the example.

How do I avoid that problem ? I want the command to be robust, so that I can put any value in the variable, and it will do as I want.

message edited by tvc

See More: redirect issue with variables

Report •

December 4, 2014 at 17:02:31
✔ Best Answer
i think this is what you want: echo.%var%>
You can also eliminate the trailing CRLF, but not from the commandline, only from batch:
set /p=%var%<nul>>
---edit: also forgot:
>> echo %var%
is actually the more standard solution, but either works, so...

message edited by nbrane

Report •

December 5, 2014 at 03:38:20
Never knew that, I do use


But then I've never thought of using it like you've shown here. Nice!

Report •

Related Solutions

Ask Question