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条回答
Rolldiameter
2楼-- · 2019-01-12 13:50

You could also use:

echo. 2>foo

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

查看更多
【Aperson】
3楼-- · 2019-01-12 13:52

copy con SomeFile.txt Enter

Ctrl-Z Enter

查看更多
男人必须洒脱
4楼-- · 2019-01-12 13:53

This worked for me,

echo > file.extension

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

sometext > filename.extension

Eg.

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楼-- · 2019-01-12 13:55

Reading comments on my post, I have to admit I didn't read the question right.

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.

查看更多
时光不老,我们不散
6楼-- · 2019-01-12 13:55

type nul>filename will create a new empty file.

Sorry I'm late.

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

查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-01-12 13:57

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
查看更多
登录 后发表回答