可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
回答1:
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!
".
回答2:
Try this:
type NUL > 1.txt
this will definitely create an empty file.
回答3:
Here's another way:
cd. > filename
回答4:
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.
回答5:
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
回答6:
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.
回答7:
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.
回答8:
call>file.txt
this is the cleanest way I know.
回答9:
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;
}
回答10:
copy con SomeFile.txt Enter
Ctrl-Z Enter
回答11:
cd > filename.cfg
worked when creating a file in C:/Program Files where you don't have the access to create files directly.
回答12:
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
.
回答13:
Yet another method that creates a zero byte file:
break > "file.txt"
回答14:
Use copy > your_file_name.extension
in command prompt
like
P:\excecise> copy > Sample.txt
回答15:
You could also use:
echo. 2>foo
The debug output for echo.
will almost definitely be empty.
回答16:
type nul>filename
will create a new empty file.
Sorry I'm late.
UPDATE: Also copy nul filename
works without redirecting (more obvious solution).
回答17:
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
回答18:
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.
回答19:
On Windows
I tried doing this
echo off > fff1.txt
and it created a file named fff1.txt with file size of 0kb
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.
回答20:
Yet another way:
copy nul 2>empty_file.txt
回答21:
I read many threads but it is not the shortest way.
Please use command:
>copy /b NUL empty_file.txt
回答22:
Just I have tried in windows
copy con file.txt
then Press Enter Key then Press Ctrl+Z Enter
And its worked for me.
For Ubuntu usually I am creating a file using VI command
vi file.txt
It will open the file then press ESC key then type :wp then press enter key. It will create a new file with empty data.
回答23:
Try this:
echo $null >> filename
See: superUser
回答24:
so you can create an empty file with
'' > newfile.txt
navigate to the directory and type the above command in PowerShell window.
Note this will not work on windows command prompt.
回答25:
Today I've discovered a new one :)
This will change the command line window title, but will also create a empty file.
title > file.txt
回答26:
Run CMD in admistrator mode and type this:
NUL > file_name.extention
or you type this
echo .> file_name.extention
回答27:
Here is yet another way:
rem/ > file.ext
The slash /
is mandatory; without it the redirection part was commented out by rem
.
回答28:
- 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.
回答29:
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
回答30:
echo.|set /p=>file
echo.
suppress the "Command ECHO activated"
|set /p=
prevent newline (and file is now 0 byte)