[英]How can I create an empty file at the command line in Windows?
How can I create an empty file at the DOS/Windows command-line?如何在 DOS/Windows 命令行中创建一个空文件?
I tried:我试过:
copy nul > file.txt
But it always displays that a file was copied.但它总是显示一个文件被复制。
Is there another method in the standard cmd?标准cmd中有另一种方法吗?
It should be a method that does not require the touch command from Cygwin or any other nonstandard commands.它应该是一种不需要 Cygwin 中的touch命令或任何其他非标准命令的方法。 The command needs to run from a script, so keystrokes cannot be used.
该命令需要从脚本运行,因此不能使用击键。
Without redirection, Luc Vu or Erik Konstantopoulos point out to:如果没有重定向,吕克·武或埃里克Konstantopoulos点出来到:
copy NUL EMptyFile.txt
copy /b NUL EmptyFile.txt
" How to create empty text file from a batch file? " (2008) also points to: “如何从批处理文件创建空文本文件? ”(2008 年)还指出:
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 : Nomad提到了一个原始的:
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:
我使用的最短的基本上是 Nomad 的:
.>out.txt
It does give an error:它确实给出了一个错误:
'.' is not recognized as an internal or external command
But this error is on stderr.但是这个错误是在 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
:或者,在Rain的回答中,重定向到
NUL
:
.>out.txt 2>NUL
(Original answer, November 2009) (原答案,2009 年 11 月)
echo.>filename
( echo ""
would actually put "" in the file! And echo
without the '.' would put " Command ECHO activated
" in the file...) (
echo ""
实际上会将 "" 放入文件中!而没有 '.' 的echo
会将“ Command ECHO activated
”放入文件中...)
Note: the resulting file is not empty but includes a return line sequence: 2 bytes.注意:生成的文件不为空,但包含一个返回行序列:2 个字节。
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 anul
response to theset/p
command, which will cause the variable used to remain unchanged.“
<nul
”将一个nul
响应传递给set/p
命令,这将导致使用的变量保持不变。 As usual withset/p
, the string to the right of the equal sign is displayed as a prompt with no CRLF.与
set/p
,等号右侧的字符串显示为没有 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
与
cd. > filename
的区别cd. > filename
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 : cd. > filename
(在Patrick Cuff 的回答中提到并且也生成了一个 0 字节长度的文件)是这个“重定向位”( <nul...
技巧)可用于在没有任何 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!
".dir
命令应将文件大小指示为 11 个字节:“helloworld!
”。
Try this:尝试这个:
type NUL > 1.txt
this will definitely create an empty file.这肯定会创建一个空文件。
这是另一种方式:
cd. > filename
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.只需将 stdout 重定向到 nul,复制的输出就会消失。
Open file:打开文件:
type file.txt
New file:新文件:
type nul > file.txt
type nul > file.txt
echo This is a sample text file > sample.txt
echo This is a sample text file > sample.txt
notepad myfile.txt
<press Enter >notepad myfile.txt
<回车> Edit content:编辑内容:
notepad file.txt
Copy复制
copy file1.txt file1Copy.txt
Rename改名
rename file1.txt file1_rename.txt
Delete file:删除文件:
del file.txt
On the Windows command-line, one way would be to use fsutil :在 Windows 命令行上,一种方法是使用fsutil :
fsutil file createnew <filename> <size>
An example:一个例子:
fsutil file createnew myEmptyFile.txt 0
Below is for *nix command-line.下面是 *nix 命令行。
touch filename
This command changes your modified date of a file or creates it if file is not found.此命令更改文件的修改日期或在未找到文件时创建它。
call > file.txt
这是我所知道的最干净的方式。
cd > filename.cfg
在 C:/Program Files 中创建文件时工作,您无权直接创建文件。
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;
}
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如果你想创建一个 JavaScript 文件,那么
type nul > demo.js
copy con SomeFile.txt
Enter copy con SomeFile.txt
回车
Ctrl + Z and Enter . Ctrl + Z和Enter 。
You can create an empty file with您可以创建一个空文件
'' > newfile.txt
Navigate to the directory and type the above command in a PowerShell window.导航到该目录并在 PowerShell 窗口中键入上述命令。
Note that this will not work on the Windows command prompt.请注意,这不适用于 Windows 命令提示符。
另一种创建零字节文件的方法:
break > "file.txt"
在命令提示符中使用copy > your_file_name.extension
像
P:\excecise> copy > Sample.txt
Try this : abc > myFile.txt
First, it will create a file with name myFile.txt
in present working directory (in command prompt).试试这个:
abc > myFile.txt
首先,它将在当前工作目录(在命令提示符下)创建一个名为myFile.txt
的文件。 Then it will run the command abc
which is not a valid command.然后它将运行命令
abc
,这不是一个有效的命令。 In this way, you have gotten a new empty file with the name myFile.txt
.这样,您就获得了一个名为
myFile.txt
的新空文件。
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. xyz
不是内部或外部命令、可运行的程序或批处理文件。
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.但我发现奇怪的是,即使该命令不是标准的 Windows 命令,也会在目录中创建文件。
type nul > filename
will create a new empty file. type nul > filename
将创建一个新的空文件。
Also copy nul filename
works without redirecting (more obvious solution).也
copy nul filename
无需重定向(更明显的解决方案)。
You could also use:您还可以使用:
echo. 2>foo
The debug output for echo.
echo.
的调试输出echo.
will almost definitely be empty.几乎肯定是空的。
I read many threads but it is not the shortest way.我阅读了很多线程,但这不是最短的方法。
Please use command:请使用命令:
>copy /b NUL empty_file.txt
Try this:尝试这个:
echo $null >> filename
See: Equivalent of Linux touch
to create an empty file with PowerShell请参阅: 等效于使用 PowerShell 创建空文件的 Linux
touch
You can use the old command您可以使用旧命令
copy con file_name.ext
Don't type anything.不要输入任何东西。 Just press F6 to save it.
只需按F6即可保存它。 However, it will print "File copied", but when you open the file, it will be empty.
但是,它会打印“文件已复制”,但是当您打开文件时,它会是空的。
echo '' > %1
(name the file as touch.bat).echo '' > %1
的 bat 文件(将文件命名为 touch.bat)。touch temp.txt
creates the temp.txt file) touch temp.txt
创建temp.txt文件) Check this article for more information.查看 这篇文章了解更多信息。
还有一种方式:
copy nul 2> empty_file.txt
On Windows在 Windows 上
I tried doing this我试过这样做
echo off > fff1.txt
And it created a file named fff1.txt with a file size of 0 KB.它创建了一个名为fff1.txt的文件,文件大小为 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.
注意:您必须位于要创建文件的目录中。
Here is yet another way:这是另一种方式:
rem/ > file.ext
The slash /
is mandatory;斜线
/
是强制性的; without it the redirection part is commented out by rem
.没有它,重定向部分被
rem
注释掉。
I have just tried in Windows:我刚刚在 Windows 中尝试过:
copy con file.txt
Then press the Enter key.然后按Enter键。 Then press Ctrl + Z and Enter .
然后按Ctrl + Z和Enter 。
And it worked for me.它对我有用。
For Ubuntu, usually I am creating a file using the vi
command对于 Ubuntu,通常我使用
vi
命令创建一个文件
vi file.txt
It will open the file.它将打开文件。 Then press the Esc key.
然后按Esc键。 Then type :wp and press the Enter key.
然后输入:wp并按Enter键。 It will create a new file with empty data.
它将创建一个包含空数据的新文件。
. >> 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
:但是,您将在
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
.您可以通过将标准错误重定向到
NUL
来抑制此错误消息(如果需要)。
. >> file.txt 2> nul
这将更改命令行窗口标题,但也会创建一个空文件。
title > file.txt
Run CMD in administrator mode and type this:以管理员模式运行 CMD 并输入:
NUL > file_name.extention
Or you type this或者你输入这个
echo .> file_name.extention
There are also other easy ways to create files.还有其他简单的方法来创建文件。
For example, you can create file and open it with notepad from cmd例如,您可以从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.不仅Notepad ,您还可以使用其他应用程序来执行此操作。 Eg, you can use Visual Studio Code to create and open new files.
例如,您可以使用Visual Studio Code创建和打开新文件。
For example,例如,
code newFile.py
This will create a new Python file and open it in vs code (if you have vs code installed).这将创建一个新的 Python 文件并在 vs 代码中打开它(如果你安装了 vs 代码)。
You can also create other types of files with this method also.您也可以使用此方法创建其他类型的文件。
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
命令用空版本覆盖创建的文件:
copy /y nul myfile.txt
echo. | set /p=>file
echo.
suppresses the "Command ECHO activated"抑制“命令 ECHO 激活”
| set /p=
| set /p=
prevents newline (and the file is now 0 byte) | set /p=
防止换行(文件现在是 0 字节)
As a solution from this question , please install gVim firstly.作为这个问题的解决方案,请先安装gVim 。 During the installation, please remember to check the option "Create .bat files for command line use" as shown below.
在安装过程中,请记得勾选“为命令行使用创建 .bat 文件”选项,如下所示。
Some bat files like vim.bat and gvim.bat will be installed under C:\\Windows .一些 bat 文件如 vim.bat 和 gvim.bat 将安装在C:\\Windows 下。 Add C:\\Windows to system Path if it's not done.
如果未完成,请将C:\\Windows添加到系统路径。
Relaunch the cmd.exe and type gvim empty_file.txt
.重新启动cmd.exe并输入
gvim empty_file.txt
。 You will launch gVim to edit the empty file from scratch.您将启动 gVim 以从头开始编辑空文件。 If you don't want to leave your "Command Prompt" console, type
vim empty_file.txt
instead.如果您不想离开“命令提示符”控制台,请输入
vim empty_file.txt
。
Create an empty file, Windows PowerShell:创建一个空文件,Windows PowerShell:
![]()
echo fileName.fileExtension<\/code><\/pre><\/blockquote>
If you want to open in Notepad:如果要在记事本中打开:
![]()
nodepad fileName.fileExtension<\/code><\/pre><\/blockquote>"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.