Can somebody remember what was the command to create an empty file in MSDOS using BAT file?
- 3Also atstackoverflow.com/questions/1702762, "How to create an empty file at the command line?".Peter Mortensen– Peter Mortensen2010-01-25 12:13:42 +00:00CommentedJan 25, 2010 at 12:13
- 6You aren't confusing DOS and
cmd.exe, are you?user unknown– user unknown2012-05-10 03:56:45 +00:00CommentedMay 10, 2012 at 3:56 - 1READ ME:
Set-Content "your_file.txt" .gitignore -Encoding utf8this is case-sensitive and forces utf8 encoding! (I also posted this as an answer).Wolfpack'08– Wolfpack'082020-01-25 06:26:07 +00:00CommentedJan 25, 2020 at 6:26 - 2Does this answer your question?How can I create an empty file at the command line in Windows?malat– malat2023-07-10 08:32:50 +00:00CommentedJul 10, 2023 at 8:32
13 Answers13
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 alist 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 preventscopy from asking a question you can't see when output goes toNUL.
9 Comments
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.CON, aside from that linked in this answer. Could someone share some light reading?echo. 2>EmptyFile.txtThis redirects output stream 2 (stderr) to a file. The commandecho doesn't output anything tostderr, so the file becomes empty.
Plainecho would work too, butecho. is better because it doesn't print the useless and potentially confusing messageECHO is on.
12 Comments
. inecho.echo 2 without the., the console would read "ECHO is off." Usingecho. 2 effectively silences console output by only displaying a newline.type NUL > EmptyFile.txtAfter 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 toNUL, like the previous post does, and it looks nice next to theECHO OutputLineFromLoop >> Emptyfile.txt that will usually follow in a batch file.
4 Comments
Techniques I gathered from other answers:
Makes a 0 byte file a very clear, backward-compatible way:
type nul >EmptyFile.txtidea via:anonymous,Danny Backett, possibly others, myself inspired byJdeBP's work
A 0 byte file another way, it's backward-compatible-looking:
REM. >EmptyFile.txtidea via:Johannes
A 0 byte file 3rd way backward-compatible-looking, too:
echo. 2>EmptyFile.txtidea via:TheSmurf
A 0 byte file the systematic wayprobably available since Windows 2000:
fsutil file createnew EmptyFile.txt 0idea via:Emm
A 0 bytes file overwriting readonly files
ATTRIB -R filename.ext>NUL(CD.>filename.ext)2>NULidea via:copyitright
A single newline (2 bytes:0x0D 0x0A inhex notation, alternatively written as\r\n):
echo.>AlmostEmptyFile.txtNote:no space betweenecho,. and>.
idea via:How can you echo a newline in batch files?
edit It seems thatany invalid commandredirected to a file would create an empty file. heh, a feature!compatibility: uknown
TheInvisibleFeature <nul >EmptyFile.txtA 0 bytes file: invalid command/ with a random name (compatibility: uknown):
%RANDOM%-%TIME:~6,5% <nul >EmptyFile.txtvia: greatsource for random by Hung Huynh
edit 2 Andriy Mpoints 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.txtidea via:Andriy M
A 0 bytes file 4th-coming way:
break > file.txtidea via:foxidrive thanks tocomment ofDouble Gras!
7 Comments
type nul ..., nottype <nul ..., actually.NonExistentCommand <nul >EmptyFile.txt and it workedbreak > file.txt, propsfoxidrive>> if you need "must exist but should not be truncated if exist" (like Unixtouch does)REM. > empty.file
4 Comments
REM., cmd will respond with "'rem.' is not recognized as an internal or external command, operable program or batch file."rem/ instead ofrem. solves that problem...If there's a possibility that the to be written file already exists and is read only, use the following code:
ATTRIB -R filename.extCD .>filename.extIf no file exists, simply do:
CD .>filename.ext(updated/changed code according to DodgyCodeException's comment)
To supress any errors that may arise:
ATTRIB -R filename.ext>NUL(CD .>filename.ext)2>NUL1 Comment
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.One more to add to the books - short and sweet to type.
break>file.txtbreak>"file with spaces in name.txt"6 Comments
fsutil file createnew file.cmd 02 Comments
You can use aTYPE command instead ofCOPY. Try this:
TYPE File1.txt>File2.txtWhereFile1.txt is empty.
2 Comments
type NUL>File2.txtYou can also useSET to create a nullbyte file as follows
set x=x > EmptyFile.txtOr if you don't want to create an extra variable reassign an existing variable like
set PROMPT=%PROMPT% > EmptyFile.txtor like this:
set "PROMPT=%PROMPT%" > EmptyFile.txtComments
There are infinite approaches.
Commands that output nothing:
breakclscolorgotopushdpopdprompttitleWeird Commands:
CD.REM.@echo offcmd /cSTART >FILEThe outdatedprint command produces a blank file:
print /d:EMPTY_TEXT_FILE nulComments
The easiest way is:
echo. > Filename.txt
2 Comments
IMPORTANT:
If you don't set the encoding, many softwares can break. git is a very popular example.
Set-Content "your_ignore_file.txt" .gitignore -Encoding utf8this is case-sensitive and forces utf8 encoding!
5 Comments
Explore related questions
See similar questions with these tags.













