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.
Author by
user541686
Updated on July 05, 2022Comments
-
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 over 12 years+1 OH MY GOSH that's brilliant! I totally didn't think of that. :)
-
user541686 over 12 yearsExcept 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 over 12 yearsYou know, I think I'll ask that as a separate question. :)
-
jeb over 12 yearsIt's weird, and it works only if there is no space/tab nor any of
,;=(
. This oneecho.hello(you2>>text.txt
fails -
Pacerier over 7 yearsBut there is still a CRLF behind (giving us 3 bytes). How can that be stopped?
-
Pacerier over 7 yearsBtw what's the logic behind this?
-
Pacerier over 7 years@Kornel, But there is still a CRLF behind (giving us 3 bytes). How can that be stopped?
-
Pacerier over 7 yearsBut there is still a CRLF behind (giving us 3 bytes). How can that be stopped?
-
Pacerier over 7 yearsBut there is still a CRLF behind (giving us 3 bytes). How can that be stopped?
-
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 over 7 yearsAwesome solution, but looks like there still might be a CRLF behind. How do we handle this unstoppable threat?
-
headinabook almost 7 yearsThis 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 almost 7 years@Pacerier -- see this answer for how to echo the number 2 to a file without the CRLF behind.
-
headinabook almost 7 yearsThe answer at this link here shows how to echo the number 2 to a file without the CRLF behind.
-
headinabook almost 7 yearsThe answer at this link here shows how to echo the number 2 to a file without the CRLF behind.
-
headinabook almost 7 yearsThe answer at this link shows how to echo the number 2 to a file without the CRLF behind.
-
headinabook almost 7 yearsThe answer at this link shows how to echo the number 2 to a file without the CRLF behind.
-
Admin over 6 years@Pacerier Are you sure that it is a requirement of the OP?
-
aschipfl about 4 yearsYou could avoid the pipe and use this:
< nul set /P ="2" > file.txt
, but thenErrorLevel
becomes1
, even with a dummy variable... -
aschipfl about 4 years
echo.
could fail if there is a file namedecho.
(no extension) in the current directory, so better useecho/
... -
aschipfl about 4 yearsWell, how does this answer the question?
-
Jan Święcki about 4 yearsI should probably rephrase my post. It is a supplement to stackoverflow.com/a/7225667/1637178.
-
aschipfl about 4 yearsSorry, I still don't get it; this is not echoing a number to a file...
-
HaxAddict1337 almost 4 years@headinabook not only is it not cleaner, it will set errorlevel to 0. (if you want 0 just
(call )
!