Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I suspect this is a particularly elementary question I'm about to ask; please forgive me. I am merely trying to echo variables to text files, but when the variable is a single-digit number, it seems to mess up how it writes to the file, as in the example below:
set FIRST=5
set SECOND=6echo %FIRST%>"C:\first.txt"
echo %SECOND%>"C:\second.txt"The resulting text files end up blank! It looks like it's adding a leading space to the variable when it tries to write to the file, which then just seems to write a blank file. The text files must only contain the numbers (no quotes or extra characters), and the numbers won't always be single-digits. Any thoughts on how/why this is happening? Thanks!!

Don't know why it's happening, but I do have a workaround:
echo.%FIRST%>"C:\first.txt"
echo.%SECOND%>"C:\second.txt"

I guess that is because this does not work either :
echo 2> test.txt
" 2>" is a system-interpreted string ...Undo that by means of this:
echo.2> test.txt
2= error output, ex. using : 2>
1=normal output, but the number 1 is not actually written, ex. 1>3, 4, ... are other reserved types ?!
Never had that problem ; never thought about it, but indeed, this will not work:
echo 2> test.txt
Hi there.

[message deleted because by the time I pressed the Post button, someone else had already posted what I was going to say]

Razor2.3 - Huzzah! Thanks a bunch; it works great!!!
User123456789 & klint - Yah, I hadn't ever run into this problem before, but that makes perfect sense. I'll remember to try to steer clear of this in the future. Thanks!

The problem is due to how NT cmd.exe processes the std out streams, i.e. 1> is stdout, 2> is stderr and more.
So if you want to echo a single digit you have to prefix it with the caret (^).
Referring to the original post the following solve the issue
If %first% lss 10 (
echo.^%first%> C:\first.txt
) else (
echo.%first%> C:\first.txt)

If you thought that was funny, how about
C:\>type x 2> y
C:\>type y
The system cannot find the file specified.C:\>dir y
Directory of C:\10/01/2008 15:17 44 y
1 File(s) 44 bytes
0 Dir(s) 5,974,595,906,560 bytes freeC:\>type y
The system cannot find the file specified.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |