Ben Collins
Ben Collins

Reputation: 221

Writing Numbers to a text file - batch file

This should be a simple one and I am dissapointed with myself but cant for the life of me find a solution! I am trying to write the number 1 to a text file in a BAT script. If I do the below it works but leaves a trailing space after the number which I cannot have. I know you can remove the trailing space by deleting the space between the 1 and the > (this works with letters) but with numbers all of a sudden I get a message saying - ECHO is OFF.

ECHO 1 > mytextfile.txt
*This works but leaves a trailing space*

ECHO 1> mytextfile.txt
*This gives me an error saying ECHO is OFF*

Upvotes: 11

Views: 6368

Answers (3)

jeb
jeb

Reputation: 82247

The problem is that echo 1> myTextFile.txt will not echo 1, instead the echo command is empty and the redirection goes to stream 1.

You could simply move the redirection to the front.

>mytextfile.txt echo 1

Upvotes: 15

David Brabant
David Brabant

Reputation: 43489

echo.1>mytextfile.txt should work as well.

Upvotes: 1

embedded.kyle
embedded.kyle

Reputation: 11466

1> is a special command. Use (ECHO 1) > mytextfile.txt instead.

Upvotes: 5

Related Questions