74

How do I echo the number 2 into a file, from a batch script?

This doesn't work:

Echo 2>> file.txt

because 2>> is a special command. :(

aschipfl
  • 33,626
  • 12
  • 54
  • 99
user541686
  • 205,094
  • 128
  • 528
  • 886

11 Answers11

340

Little-known feature: The redirection operator can go anywhere on the line.

>>file.txt echo 2
Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
74

Use (ECHO 2)>>file.txt. This will output 2 without any spaces.

Dominic K
  • 6,975
  • 11
  • 53
  • 62
36

echo ^2>>file.txt appears to work for me.

lavinio
  • 23,931
  • 5
  • 55
  • 71
  • But there is **still** a CRLF behind (giving us 3 bytes). How can that be stopped? – Pacerier Aug 04 '16 at 21:20
  • @Pacerier -- see [this answer](https://stackoverflow.com/a/38589300/3923957) for how to echo the number 2 to a file without the CRLF behind. – headinabook Jul 13 '17 at 20:21
  • @Pacerier Are you sure that it is a requirement of the OP? –  Nov 11 '17 at 11:35
31

Use the ^ escape :

Echo ^2>> file.txt
Moe Sisko
  • 11,665
  • 8
  • 50
  • 80
11
echo.2>>text.txt

Yes, it's weird.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
Kornel Kisielewicz
  • 55,802
  • 15
  • 111
  • 149
  • 4
    It's weird, and it works only if there is no space/tab nor any of `,;=(`. This one `echo.hello(you2>>text.txt` fails – jeb Sep 14 '11 at 13:19
  • @Kornel, But there is **still** a CRLF behind (giving us 3 bytes). How can that be stopped? – Pacerier Aug 04 '16 at 21:22
  • The answer at this link [here](https://stackoverflow.com/a/38589300/3923957) shows how to echo the number 2 to a file without the CRLF behind. – headinabook Jul 13 '17 at 20:25
  • `echo.` could fail if there is a file named `echo.` (no extension) in the current directory, so better use `echo/`... – aschipfl Mar 24 '20 at 23:44
10

another method

echo>>file.txt 2
walid2mi
  • 2,704
  • 15
  • 15
  • But there is **still** a CRLF behind (giving us 3 bytes). How can that be stopped? – Pacerier Aug 04 '16 at 21:22
  • The answer at this [link](https://stackoverflow.com/a/38589300/3923957) shows how to echo the number 2 to a file without the CRLF behind. – headinabook Jul 13 '17 at 20:26
6

Or you can use the delayed expansion

setlocal EnableDelayedExpansion
set "var=2"
echo(!var!>> file.txt
jeb
  • 78,592
  • 17
  • 171
  • 225
  • But there is **still** a CRLF behind (giving us 3 bytes). How can that be stopped? – Pacerier Aug 04 '16 at 21:23
  • The answer at this [link](https://stackoverflow.com/a/38589300/3923957) shows how to echo the number 2 to a file without the CRLF behind. – headinabook Jul 13 '17 at 20:26
3

If you need an accurate write, use this command

(ECHO |set /p=2)>>file.txt

the command by DMan will produce a 0x0D0A line break behind the "2"

minhng99
  • 129
  • 1
  • 6
  • 3
    This will set ERRORLEVEL to 1. If it is preferred to set ERRORLEVEL to 0, you could use something like `(echo | set /p dummyName="2") > file.txt` ([source](https://stackoverflow.com/a/11336754/3923957)). – headinabook Jul 13 '17 at 20:05
  • 1
    You could avoid the pipe and use this: `< nul set /P ="2" > file.txt`, but then `ErrorLevel` becomes `1`, even with a dummy variable... – aschipfl Mar 24 '20 at 23:42
  • @headinabook not only is it not cleaner, it will set errorlevel to 0. (if you want 0 just `(call )`! – ScriptKidd Jun 03 '20 at 14:34
2

To write a number to a file without a trailing line-break you could use the following:

cmd /C set /A "2">file.txt

This works, because set /A returns the (last) result (without line-break) when executed in command prompt context (hence when directly run in cmd.exe).

If you are working in command prompt anyway, you could simply use this:

set /A "2">file.txt

Though you cannot use that in a batch file, you need the extra cmd /C to force the right execution context.

Needless to say, this can of course only output numbers, or precisely said, signed 32-bit integers.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
-1

Based on this answer, if anyone is wondering how to do this:

echo "command1 && command2" > file.txt

with no qoutes in file.txt then you do it like this:

echo command1 ^&^& command2 > file.txt

Tested on Windows Server 2003.

Community
  • 1
  • 1
Jan Święcki
  • 1,611
  • 2
  • 16
  • 28
-1

escape the haracter '2'

in linux: echo \2>file.txt

in windows: echo ^2>file.txt

in windows this also will work

echo.2>file.txt no space between echo and the dot

milevyo
  • 2,165
  • 1
  • 13
  • 18