257

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

aschipfl
  • 28,946
  • 10
  • 45
  • 77
m_pGladiator
  • 8,010
  • 7
  • 41
  • 60

13 Answers13

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

ephemient
  • 180,829
  • 34
  • 259
  • 378
  • 36
    +1 - the question does state an *empty* file, so the accepted answer is wrong. – Joe Oct 24 '08 at 08:35
  • 10
    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 – ephemient Oct 24 '08 at 18:43
  • 2
    type nul > EmptyFile.txt is shortest solution. Still your answer is better then accepted solution, cause your file will be really empty. +1 – TPAKTOPA Jan 12 '15 at 16:14
  • 3
    Warning: 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 .) Using `type NUL >> emptyfile.txt` is safer in this regard. – eckes May 06 '15 at 03:16
  • I couldn't find any information on `CON`, aside from that linked in this answer. Could someone share some light reading? – Sinjai Jan 24 '18 at 07:41
  • @Sinjai `CON` is the console, https://en.wikipedia.org/wiki/Device_file#DOS,_TOS,_OS/2,_and_Windows – ephemient Jan 24 '18 at 17:18
  • @ephemient Awesome, do you have anything on practical uses? – Sinjai Jan 24 '18 at 17:25
  • @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, using `CON` will cause its output to go to the screen. Stuff like that. – ephemient Jan 24 '18 at 17:46
  • For me, win10, this causes syntax-error? So filepath is not valid? – Nikolai Ehrhardt Feb 02 '21 at 08:49
243
echo. 2>EmptyFile.txt
TheSmurf
  • 14,671
  • 2
  • 37
  • 47
  • 49
    This echoes a newline to stdout, though... my answer doesn't. – ephemient Oct 17 '08 at 04:08
  • 1
    That's a fairly tangential concern, I think. Not really part of the problem. – TheSmurf Oct 17 '08 at 13:48
  • 7
    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. :-) – Joey Mar 04 '09 at 03:47
  • 20
    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 – cmptrgeekken Oct 25 '09 at 15:34
  • 7
    Why is `.` in `echo.` – Ninh Pham Jan 24 '14 at 03:50
  • 3
    @Reegan If you used `echo 2` without the `.`, the console would read "ECHO is off." Using `echo. 2` effectively silences console output by only displaying a newline. – OneManBand Nov 19 '14 at 19:28
  • Sorry @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 Oct 05 '17 at 06:30
  • Sorry 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. – riderBill Oct 05 '17 at 06:34
  • it would be great if you could explain more in your answers or at least give a reference – T.Teser Dec 13 '17 at 12:50
  • Can you change the default encoding? – Wolfpack'08 Jan 24 '20 at 12:14
189
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.

Kevin K
  • 9,050
  • 2
  • 34
  • 58
70

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!

Community
  • 1
  • 1
n611x007
  • 7,979
  • 7
  • 53
  • 91
  • It's `type nul ...`, not `type – Andriy M Apr 24 '14 at 18:59
  • @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 EmptyFile.txt` and it worked – n611x007 Apr 25 '14 at 10:41
  • since it would be unsafe to rely on a hardcoded command name expecting it to be "invalid", I added a randomized command name option – n611x007 Apr 25 '14 at 10:50
  • That'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. – Andriy M Apr 25 '14 at 11:28
  • @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 – n611x007 Apr 25 '14 at 12:09
  • 2
    Yet another one: [`break > file.txt`](http://stackoverflow.com/questions/210201/how-to-create-empty-text-file-from-a-batch-file/24822936#24822936), props [foxidrive](http://stackoverflow.com/users/2299431/foxidrive) – Gras Double Jan 02 '15 at 19:05
  • 4
    And most of the commands can be used with `>>` if you need "must exist but should not be truncated if exist" (like Unix `touch` does) – eckes May 06 '15 at 03:19
26

REM. > empty.file

Johannes
  • 261
  • 3
  • 2
  • 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... – AntonyG May 23 '17 at 13:39
  • The 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 Feb 01 '18 at 11:25
  • @DodgyCodeException, using `rem/` instead of `rem.` solves that problem... – aschipfl Feb 19 '19 at 19:57
  • @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! – DodgyCodeException Feb 20 '19 at 09:44
9

If there's a possibility that the to be written file already exists and is read only, use the following code:

ATTRIB -R filename.ext
CD .>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
script'n'code
  • 195
  • 2
  • 14
  • 1
    The only problem with this solution is that if there is a file called `CD` (no extension) in the current directory, and you type `CD.`, cmd will respond with "'CD.' is not recognized as an internal or external command, operable program or batch file." If you instead type `CD .` (with a space before the dot) it will work. – DodgyCodeException Feb 01 '18 at 11:31
7

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

break>file.txt
break>"file with spaces in name.txt"
Nam G VU
  • 28,311
  • 62
  • 206
  • 338
foxidrive
  • 37,659
  • 8
  • 47
  • 67
  • I guess we woyld skip the quotes – Nam G VU Jul 24 '15 at 06:31
  • @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. – foxidrive Jul 25 '15 at 03:32
  • Yeah I agree ^^ Permission to update yours as discussed. – Nam G VU Jul 25 '15 at 06:53
  • @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. – foxidrive Jul 25 '15 at 07:37
  • 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. – kayleeFrye_onDeck Jul 17 '18 at 01:09
7
fsutil file createnew file.cmd 0
display-name-is-missing
  • 4,742
  • 4
  • 24
  • 39
Emm
  • 71
  • 1
  • 1
2

You can use a TYPE command instead of COPY. Try this:

TYPE File1.txt>File2.txt

Where File1.txt is empty.

Kevin K
  • 9,050
  • 2
  • 34
  • 58
1

You can also use SET to create a null byte 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
  • 1,561
  • 1
  • 9
  • 20
PeterE
  • 11
  • 1
0

There are infinite approaches.

Commands that output nothing:

break
cls
color
goto
pushd
popd
prompt
title

Weird Commands:

CD.
REM.
@echo off
cmd /c
START >FILE

The outdated print command produces a blank file:

print /d:EMPTY_TEXT_FILE nul
HaxAddict1337
  • 733
  • 1
  • 2
  • 17
-3

The easiest way is:

echo. > Filename.txt

  • 9
    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. – Andriy M Mar 24 '14 at 15:28
  • Worth looking at my answer (below) because it changes the encoding. – Wolfpack'08 Jan 25 '20 at 06:24
-4

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 utf8 this is case-sensitive and forces utf8 encoding!

Wolfpack'08
  • 3,412
  • 8
  • 41
  • 72
  • 4
    How 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) – jeb Jan 25 '20 at 11:49
  • @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 Feb 15 '20 at 11:15
  • 2
    @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 Sep 02 '20 at 13:57
  • @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. – Wolfpack'08 Sep 19 '20 at 05:53
  • 1
    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)… – aschipfl Sep 19 '20 at 10:00