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:58
cd > filename.cfg 

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

查看更多
欢心
3楼-- · 2019-01-12 13:58

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

查看更多
疯言疯语
4楼-- · 2019-01-12 13:59

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
查看更多
爷、活的狠高调
5楼-- · 2019-01-12 13:59
echo.|set /p=>file

echo. suppress the "Command ECHO activated"

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

查看更多
Animai°情兽
6楼-- · 2019-01-12 14:00

Try this:

type NUL > 1.txt

this will definitely create an empty file.

查看更多
Root(大扎)
7楼-- · 2019-01-12 14:00

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.

查看更多
登录 后发表回答