616

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Grendler
  • 6,327
  • 3
  • 16
  • 8
  • Just found a true batch command resulting in an empty file (0 byte): I have updated my answer. – VonC Nov 09 '09 at 18:52
  • 4
    Duplicate: http://stackoverflow.com/questions/210201, "How to create empty text file from a batch file?". (But, IMHO, the answers are better here.) – Peter Mortensen Jan 25 '10 at 12:11
  • Yes, the command copy nul > file.txt is created having the text "1 file(s) copied." Empty file is not created. – Ripon Al Wasim Sep 10 '15 at 12:19
  • 5
    This to me just proves that MS-DOS is garbage if you cannot create a plain empty file with one command. – Kellen Stuart Mar 04 '16 at 17:43
  • 4
    @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 Dec 27 '16 at 01:32
  • Don't redirect the output from copy (which is *1 file(s) copied*). Copy nul to the file: **copy nul emptyFile.txt** – riderBill Oct 04 '17 at 23:17
  • `cd > "filename.extension"` should do the job e.g `cd > file.txt` – Mahesh Jamdade Jun 02 '19 at 14:12
  • MSDos will delete any 0 byte files that you try to copy. Windows doesn't. –  Feb 10 '20 at 07:06

35 Answers35

697

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!".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    you'd actually want `echo.>filename` because it will include the space as well as the newline character. – Agent_9191 Nov 09 '09 at 18:27
  • 1
    Using the `rem` command avoids creating a file with an empty line in it. – Greg Hewgill Nov 09 '09 at 18:27
  • @Agent_9191: true, I have updated my answer. @Greg: not sure what you mean: `rem>filename` produces the same result (2 bytes) – VonC Nov 09 '09 at 18:33
  • I recall the `rem` trick worked in the past (maybe only DOS?), but apparently not today! – Greg Hewgill Nov 09 '09 at 18:43
  • 3
    Noufal Ibrahim: don't let this fool you; just see the next answer which has a much easier and equally working solution. What's done here is partially wrong in the first case (not empty but contains a line break) and way overcomplicated in the second one. – Joey Jan 13 '10 at 00:07
  • @VonC Doing Samuel's suggest, `.>out.txt` just gives an error " `'.' is not recognized as an internal or external command,` " – barlop Jul 22 '17 at 16:04
  • 2
    @barlop Yes: that is the point: it triggers some error message on stderr, but *nothing* (empty string) on stdout. And `>` redirect stdout only to `out.txt`. Hence the creation of an empty file. You can disregard the error message in this context. – VonC Jul 22 '17 at 16:08
  • Note that `copy` is a batch builtin so it won't work with e.g. Python's `subprocess.run` – xjcl Feb 15 '21 at 13:00
  • @xjcl True. I suppose you would need to call that shell in order to execute its builtin command, as in https://stackoverflow.com/a/5485749/6309. – VonC Feb 15 '21 at 13:10
  • In my particular case I've just emulated it using https://stackoverflow.com/q/1158076/2111778 – xjcl Feb 15 '21 at 13:14
298

Try this:

type NUL > 1.txt

this will definitely create an empty file.

Justin
  • 2,989
  • 1
  • 12
  • 2
169

Here's another way:

cd . > filename
Adam Liss
  • 47,594
  • 12
  • 108
  • 150
Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94
  • 3
    "cd. > filename" creates empty file else you will have filepath printed in your newly created file using "cd > filename" command. – niketan Aug 07 '17 at 10:11
  • 8
    Use this: `cd . > filename.extension` Meaning you need a space between `cd` and `.` – AIon Feb 20 '18 at 19:55
82

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.

qid
  • 1,883
  • 10
  • 15
61

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
iCrazybest
  • 2,935
  • 2
  • 24
  • 24
48
call>file.txt

This is the cleanest way I know.

cure
  • 2,588
  • 1
  • 17
  • 25
  • 2
    What is it about this that is "clean"? – GreenAsJade Jan 06 '16 at 02:57
  • 8
    it outputs nothing to the file while being easy to remember and use. – cure Sep 21 '16 at 23:11
  • How does it work? It is calling a script with an empty name(?) What is the result and possible side effects (e.g., to standard error)? Isn't there a less obscure way? Couldn't `ZZ` be used instead of `call` (not that it is less obscure)? - like [Nomad's answer](https://stackoverflow.com/questions/1702762/how-can-i-create-an-empty-file-at-the-command-line-in-windows/20237561#20237561). – Peter Mortensen May 28 '21 at 19:25
  • @PeterMortensen It makes a call to a nonexistent subroutine (because there can't be an un-named subroutine). It doesn't cause an error, which you can test by running `call && echo test` or `call || echo test`. The and/or is based on the errorlevel of the previous command. Furthermore, the answer you referenced suggesting it is a better solution, is very dangerous. as others have already noted in the comments on that answer, depending on a command not existing is terrible, because if it does exist you just ran it with no clue what it does, potentially with more permissions than it should have. – cure Jan 26 '22 at 07:20
  • @PeterMortensen bottom line, nomad's answer is dangerous, call has zero side effects and doesn't raise an error, and if you want a less obscure way, maybe use powershell or python or literally anything else? and maybe don't dig through obscure answers from nearly a decade ago about something that was archaic even back then. – cure Jan 26 '22 at 07:23
45
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kris
  • 40,604
  • 9
  • 72
  • 101
  • 2
    Apperantly, VonC's answer is better than mine, so please upvote that instead. – Kris Nov 09 '09 at 18:23
  • 5
    Unfortunately: echo "" displays double quotes and they are written to the file when stream is redirected to it. The same happens with just: echo > filename because it writes ECHO is off/on to the file as well. – Grendler Nov 09 '09 at 18:24
  • maybe you could put "@echo off" on the line before creating the file to circumvent that? – Kris Nov 09 '09 at 18:27
  • short and simple. thanks – navinrangar Nov 25 '22 at 07:53
37

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nash
  • 2,181
  • 15
  • 16
  • Unfortunately, the question specifically states, "Without the touch command from Cygwin." – qid Nov 09 '09 at 18:29
  • 3
    There exist non-Cygwin implementations of the touch command: http://unxutils.sourceforge.net/ is good. – Greg Hewgill Nov 09 '09 at 18:36
  • In *nix, I'm personally partial to a simple `> filename`, which can also be used to truncate an existing file. – Frank Farmer Jan 12 '10 at 17:54
  • `fsutil` needs administrative privileges. That's a bit much to ask for simply creating an empty file ... – Joey Jan 13 '10 at 00:03
  • @Joey Aren't all these commands a bit much to create an empty file? Even the one below with 120 points is a lot when you are used to touch. – johnny Mar 18 '15 at 18:53
14

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;
}
Paul Nathan
  • 39,638
  • 28
  • 112
  • 212
14

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Darshan Jain
  • 781
  • 9
  • 19
13
cd > filename.cfg 

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

Subhashi
  • 4,145
  • 1
  • 23
  • 22
  • What is supposed to happen? How is it circumventing basic security? Can you elaborate? Preferably, by [editing your answer](https://stackoverflow.com/posts/49040428/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen May 28 '21 at 20:14
9

copy con SomeFile.txt Enter

Ctrl + Z and Enter.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DRapp
  • 47,638
  • 12
  • 72
  • 142
  • 1
    I precised the question that the command will run from script so unfortunately any keyboard interaction does not work. Thank you anyway. – Grendler Nov 09 '09 at 18:35
9

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Aswin Sekar
  • 591
  • 6
  • 11
7

Yet another method that creates a zero byte file:

break > "file.txt"
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • How does it work? What is the theory of operation? [`break` is an unknown command](https://ss64.com/nt/), only resulting in outputting to standard error? Or something else? – Peter Mortensen May 28 '21 at 19:35
6

type nul > filename will create a new empty file.

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
EKons
  • 887
  • 2
  • 20
  • 27
6

You could also use:

echo. 2>foo

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

Craig
  • 61
  • 1
  • 1
6

Use copy > your_file_name.extension in command prompt like

P:\excecise> copy > Sample.txt
0x6773
  • 1,116
  • 1
  • 14
  • 33
6
. >> 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rain
  • 3,416
  • 3
  • 24
  • 40
5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lucky
  • 16,787
  • 19
  • 117
  • 151
  • 1
    There are more than one "handles" that console applications can read/write to. The standard ones are: (0) STDIN, (1) STDOUT, and (2) STDERR. I believe the reason your trick works here is because all of the output is going to the error handle but you're only directing STDOUT to the file. Try doing: "xyz > emptyfile.txt 2>&1" to redirect STDERR to whatever STDOUT is using, which happens to be redirecting to "emptyfile.txt".You should see the error message inside that file now. – Brent Rittenhouse Apr 27 '17 at 15:35
  • 1
    The first one will *not* result in an empty file. It will contain 13 characters (11 ordinary characters + CR + LF): *"ECHO is on."* – Peter Mortensen May 30 '21 at 15:37
5

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

Please use command:

>copy /b NUL empty_file.txt

suhao399
  • 628
  • 7
  • 11
  • Why does it work? What is the theory of operation? For example, why is the leading `>` necessary (not a rhetorical question)? – Peter Mortensen May 28 '21 at 19:43
  • > at begin of line is common to show you type text after in command line only. This copy NUL file (which contain 0 bytes). so it should work. – suhao399 Jun 28 '21 at 07:50
5

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amado Saladino
  • 2,114
  • 23
  • 25
  • How does this work? Why does it work? Is `con` some reserved thingamabob? Can you elaborate? Please respond by [editing your answer](https://stackoverflow.com/posts/50424163/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen May 28 '21 at 20:18
  • As I wrote, dun type anything, just press F6 – Amado Saladino May 29 '21 at 07:43
  • This was one of the original ways to do this, because like Peter asked, yes, Con is a reserved word. If you want to test it, try changing a directory name to CON and see how it is not pleased with your life choice. – easleyfixed Sep 02 '22 at 14:29
5
  • 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pranay Kumar
  • 2,139
  • 1
  • 15
  • 15
4

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.

Nomad
  • 57
  • 1
  • 1
  • 5
    This could be very wrong if `abc` was in the path and was a convenience function for formatting all the drives except the installation directory. – Bleeding Fingers Dec 31 '13 at 08:14
4

Try this:

echo $null >> filename 

See: Equivalent of Linux touch to create an empty file with PowerShell

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
shireef khatab
  • 977
  • 2
  • 13
  • 33
4

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
OMKAR AGRAWAL
  • 128
  • 1
  • 9
3

Yet another way:

copy nul 2> empty_file.txt
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
GadBro
  • 31
  • 1
3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ARJUN KP
  • 901
  • 8
  • 16
3

There are also other easy ways to create files.

For example, you can create file and open it with notepad from cmd

notepad newFile.txt

This will prompt you that there is no such file and if you want to create it as a new file.

You can create any kind of file like this.

For example,

notepad newFile.js

OR

notepad newFile.py

Not only Notepad, you can also use other apps to do so. E.g, you can use Visual Studio Code to create and open new files.

For example,

code newFile.py

This will create a new Python file and open it in vs code (if you have vs code installed).

You can also create other types of files with this method also.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Irfan wani
  • 4,084
  • 2
  • 19
  • 34
2

Here is yet another way:

rem/ > file.ext

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • This actually works in DOSBox, unlike many of the other solutions here. Trying to `echo` nothing into a file adds a line break, using `NUL` seems to fill up a file endlessly, and if you try executing a nonexistent command, DOSBox will redirects the actual OS response on that to the text file. – Nyerguds Jul 19 '21 at 19:07
1

First create your file so that it exists:

echo . > myfile.txt

Then overwrite the created file with an empty version using the copy command:

copy /y nul myfile.txt
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Dowopfan
  • 11
  • 1
1
echo. | set /p=>file

echo. suppresses the "Command ECHO activated"

| set /p= prevents newline (and the file is now 0 byte)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
131
  • 3,071
  • 31
  • 32
1

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

title > file.txt
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ninix
  • 11
  • 1
0

Create an empty file, Windows PowerShell:

echo fileName.fileExtension

If you want to open in Notepad:

nodepad fileName.fileExtension
Alam
  • 303
  • 1
  • 3
  • 14
-1

Run CMD in administrator mode and type this:

NUL > file_name.extention

Or you type this

echo .> file_name.extention

Or

echo .
Lekens
  • 1,823
  • 17
  • 31
  • Why is administrator mode necessary (not a rhetorical question)? Please respond by [editing your answer](https://stackoverflow.com/posts/43677896/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen May 28 '21 at 19:52
  • `echo . ` creates a file with 3 bytes,a dot and CRLF, that is not an empty file. `NUL` is not a command, it shows an error message or in case of a new Win10 a popup message appears – jeb Nov 25 '22 at 08:44
-1

echo is the command that creates a file with the text you pass it to as argument.

for example, if you want to create a file named navin.txt with Navin is a web developer in it`.

then do echo "Navin is a web developer in it" > navin.txt

in case you want your new file to be totally empty, just leave the strings empty like this- echo "" > filename.txt"

a new file will be created with the given text.

navinrangar
  • 904
  • 10
  • 20
  • The question is about creating an empty file, not a file with content. With `echo` it's not possible to create an empty file – jeb Nov 25 '22 at 08:38
  • it's possible to create an empty file with echo. I have edited the answer accordingly. – navinrangar Nov 26 '22 at 13:39
  • It's still not empty `echo ""` creates a file containing two double quotes and `\r\n`. Even on linux, with sh or bash it creates a file with one byte `\n` – jeb Nov 26 '22 at 16:53
  • You can create a zero byte file using echo with additional commands -- `echo: | tr -d '\r\n' | findstr /v "^$" > C:\zerobyte_file.txt`. Adding only `tr -d '\r\n'` or `findstr /v "^$"` creates a one byte or three byte file. – noni Apr 03 '23 at 15:49