548
votes

How can I create an empty file at the DOS/Windows command-line?

I tried:

copy nul > file.txt

But it always displays that a file was copied.

Is there another method in the standard cmd?

It should be a method that does not require the touch command from Cygwin or any other nonstandard commands. The command needs to run from a script, so keystrokes cannot be used.

30
Just found a true batch command resulting in an empty file (0 byte): I have updated my answer.VonC
Duplicate: stackoverflow.com/questions/210201, "How to create empty text file from a batch file?". (But, IMHO, the answers are better here.)Peter Mortensen
Yes, the command copy nul > file.txt is created having the text "1 file(s) copied." Empty file is not created.Ripon Al Wasim
This to me just proves that MS-DOS is garbage if you cannot create a plain empty file with one command.Kellen Stuart
@KolobCanyon: you can, of course, per the answers already posted. But do you really think the ability to quickly create an empty file should be a priority for an operating system aimed at end users and needing to run in 16K of RAM? It's not a particularly useful thing to do, after all, except in a few rare edge cases.Harry Johnston

30 Answers

636
votes

Without redirection, Luc Vu or Erik Konstantopoulos point out to:

copy NUL EMptyFile.txt
copy /b NUL EmptyFile.txt

"How to create empty text file from a batch file?" (2008) also points to:

type NUL > EmptyFile.txt
# also
echo. 2>EmptyFile.txt
copy nul file.txt > nul # also in qid's answer below
REM. > empty.file
fsutil file createnew file.cmd 0 # to create a file on a mapped drive

Nomad mentions an original one:

C:\Users\VonC\prog\tests>aaaa > empty_file
'aaaa' is not recognized as an internal or external command, operable program or batch file.

C:\Users\VonC\prog\tests>dir

 Folder C:\Users\VonC\prog\tests

27/11/2013  10:40    <REP>          .
27/11/2013  10:40    <REP>          ..
27/11/2013  10:40                 0 empty_file

In the same spirit, Samuel suggests in the comments:

the shortest one I use is basically the one by Nomad:

.>out.txt

It does give an error:

'.' is not recognized as an internal or external command

But this error is on stderr. And > only redirects stdout, where nothing have been produced.
Hence the creation of an empty file.
The error message can be disregarded here. Or, as in Rain's answer, redirected to NUL:

.>out.txt 2>NUL

(Original answer, November 2009)

echo.>filename

(echo "" would actually put "" in the file! And echo without the '.' would put "Command ECHO activated" in the file...)

Note: the resulting file is not empty but includes a return line sequence: 2 bytes.


This discussion points to a true batch solution for a real empty file:

 <nul (set/p z=) >filename

 dir filename
 11/09/2009  19:45                 0 filename
 1 file(s)                         0 bytes

The "<nul" pipes a nul response to the set/p command, which will cause the variable used to remain unchanged. As usual with set/p, the string to the right of the equal sign is displayed as a prompt with no CRLF.

Since here the "string to the right of the equal sign" is empty... the result is an empty file.


The difference with cd. > filename (which is mentioned in Patrick Cuff's answer and does also produce a 0-byte-length file) is that this "bit of redirection" (the <nul... trick) can be used to echo lines without any CR:

<nul (set/p z=hello) >out.txt
<nul (set/p z= world!) >>out.txt
dir out.txt

The dir command should indicate the file size as 11 bytes: "helloworld!".

280
votes

Try this:

type NUL > 1.txt

this will definitely create an empty file.

119
votes

Here's another way:

cd. > filename
78
votes

If you really want a totally empty file, without any output to stdout, you can cheat a little:

copy nul file.txt > nul

Just redirect stdout to nul, and the output from copy disappears.

52
votes

Open file:

type file.txt

New file:

  • Way 1: type nul > file.txt
  • Way 2: echo This is a sample text file > sample.txt
  • Way 3: notepad myfile.txt <press Enter>

Edit content:

notepad file.txt

Copy

copy file1.txt file1Copy.txt

Rename

rename file1.txt file1_rename.txt

Delete file:

del file.txt
33
votes

On the Windows command-line, one way would be to use fsutil:

fsutil file createnew <filename> <size>

An example:

fsutil file createnew myEmptyFile.txt 0

Below is for *nix command-line.

touch filename

This command changes your modified date of a file or creates it if file is not found.

33
votes
call > file.txt

This is the cleanest way I know.

32
votes
echo "" > filename

I believe this works on Windows/DOS, but my last hands-on experience with either is quite a while ago. I do know for a fact that it works on basically any POSIX compliant OS.

14
votes
cd > filename.cfg 

worked when creating a file in C:/Program Files where you don't have the access to create files directly.

12
votes

You can write your own touch.

//touch.cpp
#include <fstream>
#include <iostream>

int main(int argc, char ** argv;)
{
  if(argc !=2)
  {
    std::cerr << "Must supply a filename as argument" << endl;
    return 1;
  }
  std::ofstream foo(argv[1]);
  foo.close();
  return 0;
}
8
votes

For creating any type of file you can use the following code

type nul > (file_name).(file_type)

For example, if you want to create a text file then

type nul > demo.txt

If you want to create a JavaScript file then

type nul > demo.js
7
votes

copy con SomeFile.txt Enter

Ctrl + Z and Enter.

7
votes

You can create an empty file with

'' > newfile.txt

Navigate to the directory and type the above command in a PowerShell window.

Note that this will not work on the Windows command prompt.

6
votes

Yet another method that creates a zero byte file:

break > "file.txt"
6
votes

Use copy > your_file_name.extension in command prompt like

P:\excecise> copy > Sample.txt
5
votes

You could also use:

echo. 2>foo

The debug output for echo. will almost definitely be empty.

5
votes

Try this :abc > myFile.txt First, it will create a file with name myFile.txt in present working directory (in command prompt). Then it will run the command abc which is not a valid command. In this way, you have gotten a new empty file with the name myFile.txt.

5
votes

This worked for me,

echo > file.extension

Here's another way I found today. I got ideas from other answers, but it worked:

sometext > filename.extension

For example,

xyz > emptyfile.txt  //this would create an empty zero byte text file
abc > filename.mp4   //this would create an zero byte MP4 video media file

This would show an error message in the command prompt that,

xyz is not as an internal or external command, operable program or batch file.

But the weird thing I found was the file is being created in the directory even if the command is not a standard Windows command.

5
votes

type nul > filename will create a new empty file.

Also copy nul filename works without redirecting (more obvious solution).

4
votes

I read many threads but it is not the shortest way.

Please use command:

>copy /b NUL empty_file.txt

4
votes

You can use the old command

copy con file_name.ext

Don't type anything. Just press F6 to save it. However, it will print "File copied", but when you open the file, it will be empty.

3
votes

Yet another way:

copy nul 2> empty_file.txt
3
votes

On Windows

I tried doing this

echo off > fff1.txt

And it created a file named fff1.txt with a file size of 0 KB.

I didn't find any commands other than this that could create a empty file.

Note: You have to be in the directory you wish to create the file.

3
votes
  • Create a bat file with content echo '' > %1 (name the file as touch.bat).
  • Add the folder to the PATH environment variable.
  • You can use touch to create files. (for example: touch temp.txt creates the temp.txt file)

Check this article for more information.

2
votes

Here is yet another way:

rem/ > file.ext

The slash / is mandatory; without it the redirection part is commented out by rem.

2
votes

I have just tried in Windows:

copy con file.txt

Then press the Enter key. Then press Ctrl + Z and Enter.

And it worked for me.

For Ubuntu, usually I am creating a file using the vi command

vi file.txt

It will open the file. Then press the Esc key. Then type :wp and press the Enter key. It will create a new file with empty data.

2
votes
. >> file.txt
  • >> appends standard output into a file
  • . is just a wrong command to pass the empty standard output to >>

However, you'll see standard error's output in the CMD:

'.' is not recognized as an internal or external command, operable program or batch file.

You can suppress this error message (if you want) by redirecting standard error to NUL.

. >> file.txt 2> nul
1
votes

This will change the command line window title, but it will also create a empty file.

title > file.txt
1
votes

Run CMD in administrator mode and type this:

NUL > file_name.extention

Or you type this

echo .> file_name.extention