How to create empty text file from a batch file?
Solution 1
echo. 2>EmptyFile.txt
Solution 2
copy NUL EmptyFile.txt
DOS has a few special files (devices, actually) that exist in every directory, NUL
being the equivalent of UNIX's /dev/null
: it's a magic file that's always empty and throws away anything you write to it. Here's a list of some others; CON
is occasionally useful as well.
To avoid having any output at all, you can use
copy /y NUL EmptyFile.txt >NUL
/y
prevents copy
from asking a question you can't see when output goes to NUL
.
Solution 3
type NUL > EmptyFile.txt
After reading the previous two posts, this blend of the two is what I came up with. It seems a little cleaner. There is no need to worry about redirecting the "1 file(s) copied." message to NUL
, like the previous post does, and it looks nice next to the ECHO OutputLineFromLoop >> Emptyfile.txt
that will usually follow in a batch file.
Solution 4
Techniques I gathered from other answers:
Makes a 0 byte file a very clear, backward-compatible way:
type nul >EmptyFile.txt
idea via: anonymous, Danny Backett, possibly others, myself inspired by JdeBP's work
A 0 byte file another way, it's backward-compatible-looking:
REM. >EmptyFile.txt
idea via: Johannes
A 0 byte file 3rd way backward-compatible-looking, too:
echo. 2>EmptyFile.txt
idea via: TheSmurf
A 0 byte file the systematic way probably available since Windows 2000:
fsutil file createnew EmptyFile.txt 0
idea via: Emm
A 0 bytes file overwriting readonly files
ATTRIB -R filename.ext>NUL
(CD.>filename.ext)2>NUL
idea via: copyitright
A single newline (2 bytes: 0x0D 0x0A
in hex notation, alternatively written as \r\n
):
echo.>AlmostEmptyFile.txt
Note: no space between echo
, .
and >
.
idea via: How can you echo a newline in batch files?
edit It seems that any invalid command redirected to a file would create an empty file. heh, a feature! compatibility: uknown
TheInvisibleFeature <nul >EmptyFile.txt
A 0 bytes file: invalid command/ with a random name (compatibility: uknown):
%RANDOM%-%TIME:~6,5% <nul >EmptyFile.txt
via: great source for random by Hung Huynh
edit 2 Andriy M points out the probably most amusing/provoking way to achieve this via invalid command
A 0 bytes file: invalid command/ the funky way (compatibility: unknown)
*>EmptyFile.txt
idea via: Andriy M
A 0 bytes file 4th-coming way:
break > file.txt
idea via: foxidrive thanks to comment of Double Gras!
Solution 5
REM. > empty.file
Related videos on Youtube
m_pGladiator
I don't know what to say here :) Maybe I should ask a question in StackOverflow about this field :)
Updated on May 08, 2020Comments
-
m_pGladiator about 4 years
Can somebody remember what was the command to create an empty file in MSDOS using BAT file?
-
Peter Mortensen over 14 yearsAlso at stackoverflow.com/questions/1702762, "How to create an empty file at the command line?".
-
user unknown about 12 yearsYou aren't confusing DOS and
cmd.exe
, are you? -
Wolfpack'08 over 4 yearsREAD ME:
Set-Content "your_file.txt" .gitignore -Encoding utf8
this is case-sensitive and forces utf8 encoding! (I also posted this as an answer).
-
-
ephemient over 15 yearsThis echoes a newline to stdout, though... my answer doesn't.
-
TheSmurf over 15 yearsThat's a fairly tangential concern, I think. Not really part of the problem.
-
Joe over 15 years+1 - the question does state an empty file, so the accepted answer is wrong.
-
ephemient over 15 yearsDannySmurf's solution actually does create an empty file -- a newline goes to stdout, nothing goes to stderr (directed into the new file). But thanks for the +1 anyways
-
cmptrgeekken over 14 yearsTo merge ephemient's answer and this one, you could do: "echo. >NUL 2>EmptyFile.txt" to achieve the same results without outputting a newline
-
Amit Naidu almost 13 years+1 this is the natural one that first comes to mind, not the contrivances with stderr etc.
-
Danny Beckett over 11 yearsYou can also do
type NUL>File2.txt
-
djangofan about 11 yearsYou are correct. This method avoides the /y flag on the copy command.
-
TomeeNS over 10 yearsBTW, how to get File1.txt?
-
Ninh Pham over 10 yearsWhy is
.
inecho.
-
Andriy M about 10 yearsThis will create a file with a space character followed by a carriage return character followed by a new line character – not an empty file.
-
Andriy M about 10 yearsIt's
type nul ...
, nottype <nul ...
, actually. -
n611x007 about 10 years@AndriyM thanks you're right I made the edit! Didn't notice because interestingly enough it works with the wrong one. which may mean that any invalid command redirected to a filename would create an empty file! just tried with
NonExistentCommand <nul >EmptyFile.txt
and it worked -
n611x007 about 10 yearssince it would be unsafe to rely on a hardcoded command name expecting it to be "invalid", I added a randomized command name option
-
Andriy M about 10 yearsThat's interesting. As for random command name generation, that might be unnecessary, you could just use
*>EmptyFile.txt
or anything where the "command" contains a character highly unlikely to be part of the name. I admit, though, that there's always a tiny risk it becomes a valid command in the future. -
n611x007 about 10 years@AndriyM now thats fun. You could even make it self-documenting with
?>EmptyFile.txt
;) -?
as in "what?!?!". Well, you could actually do?!?!>EmptyFile.txt
or?????>EmptyFile.txt
, to make it clear. :P -
OneManBand over 9 years@Reegan If you used
echo 2
without the.
, the console would read "ECHO is off." Usingecho. 2
effectively silences console output by only displaying a newline. -
Gras Double over 9 yearsYet another one:
break > file.txt
, props foxidrive -
TPAKTOPA over 9 yearstype nul > EmptyFile.txt is shortest solution. Still your answer is better then accepted solution, cause your file will be really empty. +1
-
Prashant about 9 yearsWarning: yes the question was on how to create an empty file, however usually you want to "make sure the file exists". When using the
copy /Y NUL
command it might erase existing content. If you are used to Unix'touch
command, this might not at all be what you expect .) Usingtype NUL >> emptyfile.txt
is safer in this regard. -
Prashant about 9 yearsAnd most of the commands can be used with
>>
if you need "must exist but should not be truncated if exist" (like Unixtouch
does) -
Nam G VU almost 9 yearsI guess we woyld skip the quotes
-
foxidrive almost 9 years@NamGVU The quotes are used in the target filename, if you choose to use long filename elements such as space or & etc. They are not part of the file contents.
-
Nam G VU almost 9 yearsYeah I agree ^^ Permission to update yours as discussed.
-
foxidrive almost 9 years@NamGVU The quotes are able to be left in place with or without a long filename. I guess if it's typed from the command line with a short filename, then you wouldn't want unnecessary typing.
-
Christian St. over 7 yearsShort and easy. A few other solutions here do not create a new file of zero bytes!
-
AntonyG about 7 yearsI'm stuck on Windows CE, and this is the only answer that creates and empty file! The other approaches either add a blank line or simply don't work...
-
riderBill over 6 yearsSorry @ephemient. I didn't see your answer before the comment editing time limit ran out. I should have given you props in my first comment. I'll upvote your answer to make amends. ;^J
-
riderBill over 6 yearsSorry for the downvote--I was in too much of a hurry. SO won't let me undo it. I'll come back later and try again. I still don't like your answer, though--the new line to stdout is annoying. Anyway, there are better solutions.
-
T.Teser over 6 yearsit would be great if you could explain more in your answers or at least give a reference
-
Sinjai over 6 yearsI couldn't find any information on
CON
, aside from that linked in this answer. Could someone share some light reading? -
ephemient over 6 years@Sinjai
CON
is the console, en.wikipedia.org/wiki/Device_file#DOS,_TOS,_OS/2,_and_Windows -
Sinjai over 6 years@ephemient Awesome, do you have anything on practical uses?
-
ephemient over 6 years@Sinjai
copy /y CON file.txt
lets you type several lines, ending with ^Z, to create a file without opening an editor. If a command normally prints to a file, usingCON
will cause its output to go to the screen. Stuff like that. -
DodgyCodeException over 6 yearsThe only problem with this solution is that if there is a file called REM in the current directory, and you type
REM.
, cmd will respond with "'rem.' is not recognized as an internal or external command, operable program or batch file." -
DodgyCodeException over 6 yearsThe FSUTIL utility requires that you have administrative privileges.
-
DodgyCodeException over 6 yearsThe only problem with this solution is that if there is a file called
CD
(no extension) in the current directory, and you typeCD.
, cmd will respond with "'CD.' is not recognized as an internal or external command, operable program or batch file." If you instead typeCD .
(with a space before the dot) it will work. -
Bernhard Eriksson almost 6 yearsfsutil is not a standard utility in MS-DOS.
-
kayleeFrye_onDeck almost 6 yearsI suggest removing the second line from this answer. IMO this is the best answer on the question so far, but the second line doesn't really add value unless the person trying to use it doesn't know the basics of white-space handling for command-line.
-
aschipfl over 5 years@DodgyCodeException, using
rem/
instead ofrem.
solves that problem... -
DodgyCodeException over 5 years@aschipfl thanks, that's interesting. It's also interesting how this answer from 2010 has comments spaced from each other by several years. A slowwwww conversation!
-
Wolfpack'08 over 4 yearsCan you change the default encoding?
-
Wolfpack'08 over 4 yearsWorth looking at my answer (below) because it changes the encoding.
-
jeb over 4 yearsHow is the encoding relevant for creating an empty file? A null byte file doesn't need any encoding and it's tricky to mark it with a BOM (0 bytes)
-
Wolfpack'08 over 4 years@jeb because people fill empty files, and when filled files are used as settings files, they create errors when they are not UTF8. In fact, I would say people almost always fill files and don't leave them blank, and I wouldn't use any other method to create a blank file that I later intended to alter because I nearly always want to be certain that my files are UTF8. For example, the .git tutorial on github asks the user to make a blank file which is filled later, and the other method makes a file with weird Windows encoding.
-
aschipfl over 3 years@Wolfpack'08, the file encoding is not stored in a special attribute or in some other magical secret area, it is just reflected by the data it contains; if there is no data, there cannot be an encoding…
-
cbmeeks over 3 yearsThis should have been the accepted answer. About as clean and simple as it gets.
-
Wolfpack'08 over 3 years@aschipfl A blank file must contain some data, then. If you create a blank file and explicitly flag utf8, the file works after it is later populated; on the other hand, if you create a blank file and ostensibly allow default encoding (ASCII), when you later fill the file it will break whatever program requires UT8 encoding. I have first-hand experience of this, and anybody can test it. Regardless of your incredulity, this is demonstrably true and tolerates empirical testing: I think the negative feedback is totally unwarranted and damages the accessibility of this important info. Prove it.
-
aschipfl over 3 yearsA blank file is a blank file, there is no data inside; otherwise, it would not be blank (empty, 0 bytes), would it? You seem to work with a versioning tool (git), which stores some meta-data for each file, apparently including its encoding. But the blank file itself definitely does not hold any encoding setting, neither in the data area (0 bytes obviously hold no information), nor in the file table of the file system (FAT, NTFS, or whatever you may use)…
-
Nikolai Ehrhardt over 3 yearsFor me, win10, this causes syntax-error? So filepath is not valid?