How to create an empty file at the command line in

2019-01-12 13:14发布

How to 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 any other 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条回答
祖国的老花朵
2楼-- · 2019-01-12 13:36

Yet another method that creates a zero byte file:

break > "file.txt"
查看更多
放荡不羁爱自由
3楼-- · 2019-01-12 13:36

Try this:

echo $null >> filename 

See: superUser

查看更多
做自己的国王
4楼-- · 2019-01-12 13:37

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

Please use command:

>copy /b NUL empty_file.txt

查看更多
来,给爷笑一个
5楼-- · 2019-01-12 13:38

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.


(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 12 bytes: "hello world!".

查看更多
仙女界的扛把子
6楼-- · 2019-01-12 13:38
call>file.txt

this is the cleanest way I know.

查看更多
在下西门庆
7楼-- · 2019-01-12 13:38
  • create a bat file with content echo '' > %1. (name the file as touch.bat)
  • add the folder to PATH variable.
  • you can use touch to create files. (Ex: touch temp.txt creates temp.txt file)

check this article for more information.

查看更多
登录 后发表回答