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% > test.xxx

That all works :

C:\>type test.xxx
1

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%> test.xxx

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

And that indeed, results in :

C:\>type test.xxx
ECHO is on.

C:\>

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 •

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

message edited by nbrane


Report •

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

echo.

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


Report •
Related Solutions


Ask Question