294

Can somebody remember what was the command to create an empty file in MSDOS using BAT file?

aschipfl's user avatar
aschipfl
35.3k12 gold badges62 silver badges106 bronze badges
askedOct 16, 2008 at 20:38
m_pGladiator's user avatar
4
  • 3
    Also atstackoverflow.com/questions/1702762, "How to create an empty file at the command line?".CommentedJan 25, 2010 at 12:13
  • 6
    You aren't confusing DOS andcmd.exe, are you?CommentedMay 10, 2012 at 3:56
  • 1
    READ ME:Set-Content "your_file.txt" .gitignore -Encoding utf8 this is case-sensitive and forces utf8 encoding! (I also posted this as an answer).CommentedJan 25, 2020 at 6:26
  • 2
    Does this answer your question?How can I create an empty file at the command line in Windows?CommentedJul 10, 2023 at 8:32

13 Answers13

413
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.

answeredOct 17, 2008 at 4:07
ephemient's user avatar
Sign up to request clarification or add additional context in comments.

9 Comments

+1 - the question does state anempty file, so the accepted answer is wrong.
DannySmurf'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
type nul > EmptyFile.txt is shortest solution. Still your answer is better then accepted solution, cause your file will be really empty. +1
Warning: yes the question was on how to create an empty file, however usually you want to "make sure the file exists". When using thecopy /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.
I couldn't find any information onCON, aside from that linked in this answer. Could someone share some light reading?
|
259
echo. 2>EmptyFile.txt

This 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.

anatolyg's user avatar
anatolyg
28.5k9 gold badges66 silver badges149 bronze badges
answeredOct 16, 2008 at 20:39
TheSmurf's user avatar

12 Comments

This echoes a newline to stdout, though... my answer doesn't.
Sometimes it's relevant; I used to have touch lying around until I got the idea of just copying NUL (or type NUL>file) for the purpose of getting 0-byte files. :-)
To merge ephemient's answer and this one, you could do: "echo. >NUL 2>EmptyFile.txt" to achieve the same results without outputting a newline
Why is. inecho.
@Reegan If you usedecho 2 without the., the console would read "ECHO is off." Usingecho. 2 effectively silences console output by only displaying a newline.
|
208
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 toNUL, like the previous post does, and it looks nice next to theECHO OutputLineFromLoop >> Emptyfile.txt that will usually follow in a batch file.

Kevin K's user avatar
Kevin K
9,5943 gold badges40 silver badges65 bronze badges
answeredNov 17, 2008 at 10:03

4 Comments

+1 this is the natural one that first comes to mind, not the contrivances with stderr etc.
You are correct. This method avoides the /y flag on the copy command.
Short and easy. A few other solutions here do not create a new file of zero bytes!
This should have been the accepted answer. About as clean and simple as it gets.
84

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 byJdeBP'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 wayprobably 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 inhex notation, alternatively written as\r\n):

echo.>AlmostEmptyFile.txt

Note: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.txt

A 0 bytes file: invalid command/ with a random name (compatibility: uknown):

%RANDOM%-%TIME:~6,5% <nul >EmptyFile.txt

via: 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.txt

idea via:Andriy M

A 0 bytes file 4th-coming way:

break > file.txt

idea via:foxidrive thanks tocomment ofDouble Gras!

answeredApr 18, 2014 at 16:37
n611x007's user avatar

7 Comments

It'stype nul ..., nottype <nul ..., actually.
@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 withNonExistentCommand <nul >EmptyFile.txt and it worked
since it would be unsafe to rely on a hardcoded command name expecting it to be "invalid", I added a randomized command name option
Yet another one:break > file.txt, propsfoxidrive
And most of the commands can be used with>> if you need "must exist but should not be truncated if exist" (like Unixtouch does)
|
27

REM. > empty.file

answeredApr 12, 2010 at 9:13
Johannes's user avatar

4 Comments

I'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...
The only problem with this solution is that if there is a file called REM in the current directory, and you typeREM., cmd will respond with "'rem.' is not recognized as an internal or external command, operable program or batch file."
@DodgyCodeException, usingrem/ instead ofrem. solves that problem...
@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!
11

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.ext

If 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>NUL
answeredFeb 26, 2012 at 23:30
script'n'code's user avatar

1 Comment

The only problem with this solution is that if there is a file calledCD (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.
9

One more to add to the books - short and sweet to type.

break>file.txtbreak>"file with spaces in name.txt"
Nam G VU's user avatar
Nam G VU
35.8k79 gold badges248 silver badges399 bronze badges
answeredJul 18, 2014 at 10:32
foxidrive's user avatar

6 Comments

I guess we woyld skip the quotes
@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.
Yeah I agree ^^ Permission to update yours as discussed.
@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.
I 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.
|
7
fsutil file createnew file.cmd 0
display-name-is-missing's user avatar
display-name-is-missing
4,4055 gold badges31 silver badges42 bronze badges
answeredMar 31, 2011 at 22:38
Emm's user avatar

2 Comments

The FSUTIL utility requires that you have administrative privileges.
fsutil is not a standard utility in MS-DOS.
2

You can use aTYPE command instead ofCOPY. Try this:

TYPE File1.txt>File2.txt

WhereFile1.txt is empty.

Kevin K's user avatar
Kevin K
9,5943 gold badges40 silver badges65 bronze badges
answeredAug 25, 2009 at 19:39

2 Comments

You can also dotype NUL>File2.txt
BTW, how to get File1.txt?
1

You can also useSET to create a nullbyte file as follows

set x=x > EmptyFile.txt

Or if you don't want to create an extra variable reassign an existing variable like

set PROMPT=%PROMPT% > EmptyFile.txt

or like this:

set "PROMPT=%PROMPT%" > EmptyFile.txt
Sunil Hari's user avatar
Sunil Hari
1,7941 gold badge12 silver badges20 bronze badges
answeredAug 7, 2014 at 11:54
PeterE's user avatar

Comments

1

There are infinite approaches.

Commands that output nothing:

breakclscolorgotopushdpopdprompttitle

Weird Commands:

CD.REM.@echo offcmd /cSTART >FILE

The outdatedprint command produces a blank file:

print /d:EMPTY_TEXT_FILE nul
answeredMar 8, 2020 at 0:50
Jeff Wu's user avatar

Comments

-3

The easiest way is:

echo. > Filename.txt

answeredMar 10, 2014 at 22:16
Batchman's user avatar

2 Comments

This will create a file with a space character followed by a carriage return character followed by a new line character –not an empty file.
Worth looking at my answer (below) because it changes the encoding.
-3

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!

answeredJan 25, 2020 at 6:27
Wolfpack'08's user avatar

5 Comments

How is the encoding relevant for creating anempty file? A null byte file doesn't need any encoding and it's tricky to mark it with a BOM (0 bytes)
@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.
@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…
@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.
A 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)…
Protected question. To answer this question, you need to have at least 10 reputation on this site (not counting theassociation bonus). The reputation requirement helps protect this question from spam and non-answer activity.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.