@echo off on DOS (cmd)

I am trying to write a BAT script, and I have the following:

@echo off REM Comments here SETLOCAL ENABLEDELAYEDEXPANSION set PROG_ROOT=C:\Prog set ONE=1 echo 1>> %PROG_ROOT\test.txt echo %ONE%>> %PROG_ROOT\test.txt for /f "tokens=*" %%f in (folders.txt) do ( echo %%f>> %PROG_ROOT\test.txt ) ENDLOCAL 

My folders.txt file contains the number "5".

My output is test.txt

 ECHO is off ECHO is off 5 

I don’t understand why the first 2 lines of the output have β€œECHO off” and the third line is printed correctly. How to print the correct conclusion?

ETA: I tried

 echo 1>> %PROG_ROOT\test.txt echo %ONE% >> %PROG_ROOT\test.txt 

and I was able to print

 ECHO is off 1 

However, I need NOT to print the trailing space after the number.

+8
dos echo
source share
1 answer

1> (and generally n> for any digit n ) is interpreted as a redirection, and therefore echo 1>> cmd appears as echo with no arguments. echo with no arguments will display the current state of echo (here ECHO is off ).

To fix, print an integer with the ^ character:

 echo ^1>> %PROG_ROOT\test.txt 
+6
source share

All Articles