Curious why some data dosen't echo to file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Curious why some data dosen't echo to file

#1 Post by Jer » 17 Jun 2015 12:08

I am asking this out of curiosity. Why does the echo command behave this way?
Thanks.

Code: Select all

@Echo Off

TYPE NUL>tmp.tmp
Set var1=1
Set /A var2=3

Echo A>>tmp.tmp
Echo 1>>tmp.tmp
Echo %var1%>>tmp.tmp
Echo %var2%>>tmp.tmp

Echo.& Echo file contents:& Echo.& Type tmp.tmp


C:\Temp>
ECHO is off.

file contents:

A
ECHO is off.
ECHO is off.
C:\Temp>

penpen
Expert
Posts: 1992
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Curious why some data dosen't echo to file

#2 Post by penpen » 17 Jun 2015 13:33

With "1>>" you advise the command processor to redirect all output of handle number 1 (default: STDOUT; allowed 0 to 9).
For more info see:
https://technet.microsoft.com/de-de/library/bb490982.aspx

You may avoid this behaviour by adding an empty command line token (so it divides the data from the command), or
you could add a space to the output (handled as data), or you could use the redirection as a prefix:

Code: Select all

for %%a in ("") do echo 1%%~a>> "tmp.tmp"
echo 1 >> "tmp.tmp"
>> "tmp.tmp" echo 1
echo Empty line:
echo(
echo end


penpen

Jer
Posts: 177
Joined: 23 Nov 2014 17:13
Location: California USA

Re: Curious why some data dosen't echo to file

#3 Post by Jer » 18 Jun 2015 12:35

You've shown three ways to echo a single digit to a file. The information explains
the echo command's special use of the digits 0-9. That clears up that riddle for me.
Here's one more way to echo a digit to file, that I found through experimenting:

Code: Select all

@Echo Off
Type NUL>"tmp.tmp"
Set var=9
setlocal enabledelayedexpansion& Echo !var!>>tmp.tmp& endlocal
Type tmp.tmp

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Curious why some data dosen't echo to file

#4 Post by Aacini » 18 Jun 2015 16:09

Another one!

Code: Select all

for %%a in (1) do echo %%a>> tmp.tmp

Antonio

Post Reply