How to echo "2" (no quotes) to a file, from a batch script?

33,431

Solution 1

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

Solution 2

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

>>file.txt echo 2

Solution 3

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

Solution 4

Use the ^ escape :

Echo ^2>> file.txt

Solution 5

echo.2>>text.txt

Yes, it's weird.

Share:
33,431
user541686
Author by

user541686

Updated on July 05, 2022

Comments

  • user541686
    user541686 almost 2 years

    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. :(

  • user541686
    user541686 over 12 years
    +1 OH MY GOSH that's brilliant! I totally didn't think of that. :)
  • user541686
    user541686 over 12 years
    Except when it has parentheses (right?). :P Now (this is a separate question, obviously) how do I echo an arbitrary file name to another file? It could have parentheses, quotes, numbers, etc...
  • user541686
    user541686 over 12 years
    You know, I think I'll ask that as a separate question. :)
  • jeb
    jeb over 12 years
    It's weird, and it works only if there is no space/tab nor any of ,;=(. This one echo.hello(you2>>text.txt fails
  • Pacerier
    Pacerier over 7 years
    But there is still a CRLF behind (giving us 3 bytes). How can that be stopped?
  • Pacerier
    Pacerier over 7 years
    Btw what's the logic behind this?
  • Pacerier
    Pacerier over 7 years
    @Kornel, But there is still a CRLF behind (giving us 3 bytes). How can that be stopped?
  • Pacerier
    Pacerier over 7 years
    But there is still a CRLF behind (giving us 3 bytes). How can that be stopped?
  • Pacerier
    Pacerier over 7 years
    But there is still a CRLF behind (giving us 3 bytes). How can that be stopped?
  • Moe Sisko
    Moe Sisko over 7 years
    @Pacerier - the "2" in this context would have a special meaning. So the ^ escape character is used to treat the "2" like a normal character.
  • My Stack Overfloweth
    My Stack Overfloweth over 7 years
    Awesome solution, but looks like there still might be a CRLF behind. How do we handle this unstoppable threat?
  • headinabook
    headinabook almost 7 years
    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).
  • headinabook
    headinabook almost 7 years
    @Pacerier -- see this answer for how to echo the number 2 to a file without the CRLF behind.
  • headinabook
    headinabook almost 7 years
    The answer at this link here shows how to echo the number 2 to a file without the CRLF behind.
  • headinabook
    headinabook almost 7 years
    The answer at this link here shows how to echo the number 2 to a file without the CRLF behind.
  • headinabook
    headinabook almost 7 years
    The answer at this link shows how to echo the number 2 to a file without the CRLF behind.
  • headinabook
    headinabook almost 7 years
    The answer at this link shows how to echo the number 2 to a file without the CRLF behind.
  • Admin
    Admin over 6 years
    @Pacerier Are you sure that it is a requirement of the OP?
  • aschipfl
    aschipfl about 4 years
    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
    aschipfl about 4 years
    echo. could fail if there is a file named echo. (no extension) in the current directory, so better use echo/...
  • aschipfl
    aschipfl about 4 years
    Well, how does this answer the question?
  • Jan Święcki
    Jan Święcki about 4 years
    I should probably rephrase my post. It is a supplement to stackoverflow.com/a/7225667/1637178.
  • aschipfl
    aschipfl about 4 years
    Sorry, I still don't get it; this is not echoing a number to a file...
  • HaxAddict1337
    HaxAddict1337 almost 4 years
    @headinabook not only is it not cleaner, it will set errorlevel to 0. (if you want 0 just (call )!