如何在 Windows 的命令行中创建一个空文件? [英] How can I create an empty file at the command line in Windows?
问题描述
如何在 DOS/Windows 命令行中创建一个空文件?
我试过了:
copy nul >文件.txt
但是总是显示文件被复制了.
标准 cmd 中还有其他方法吗?
它应该是一种不需要来自 Cygwin 的 touch 命令的方法或任何其他非标准命令.该命令需要从脚本运行,因此不能使用按键.
没有重定向,LucVu 或 Erik Konstantopoulos 指向 出:
复制NUL EmptyFile.txt复制/b NUL EmptyFile.txt
"如何从批处理文件创建空文本文件?"(2008) 还指出:
type NUL >空文件.txt# 还回声.2>空文件.txt复制空文件.txt >nul #也在下面qid的回答中快速眼动.>空的文件fsutil file createnew file.cmd 0 # 在映射驱动器上创建文件
C:\Users\VonC\prog\tests>aaaa >空的文件'aaaa' 不是内部或外部命令,也不是可运行的程序或批处理文件.C:\Users\VonC\prog\tests>dir文件夹 C:\Users\VonC\prog\tests27/11/2013 10:40 <REP>.27/11/2013 10:40 <REP>..27/11/2013 10:40 0 空文件
<块引用>我使用的最短的基本上是 Nomad 的:
.>out.txt
它确实给出了错误:
'.'不被识别为内部或外部命令
但是这个错误是在 stderr 上.并且 >
仅重定向标准输出,其中 没有 产生.
因此创建了一个空文件.
此处可以忽略错误消息.或者,如 Rain 的 answer,重定向到 NUL
:
.>out.txt 2>NUL
(原答案,2009 年 11 月)
echo.> 文件名
(echo ""
实际上会将 "" 放入文件中!并且 echo
没有 '.' 会放入 "Command ECHO激活
"在文件中...)
注意:生成的文件不是空,而是包含一个返回行序列:2 个字节.
这个讨论指向真正的空文件的真正批处理解决方案:
文件名目录文件名11/09/2009 19:45 0 文件名1 个文件 0 字节
<块引用>
nul
响应传送到 set/p
命令,这将导致变量用于保持不变.像通常的 set/p
一样,字符串到等号的右边显示为没有 CRLF 的提示.
从这里开始,等号右边的字符串"为空...结果是一个空文件.
与cd的区别.>文件名
(在 Patrick Cuff 的回答 并且确实产生了一个 0 字节长度的文件) 是这种重定向位"(
out.txt<nul (set/p z= world!) >>out.txt目录输出.txt
<块引用>
dir
命令应将文件大小指示为 11 个字节:helloworld!
".
How can I 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 another 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.
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. Or, as in Rain's answer, redirected to NUL
:
.>out.txt 2>NUL
(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 anul
response to theset/p
command, which will cause the variable used to remain unchanged. As usual withset/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 11 bytes: "helloworld!
".
这篇关于如何在 Windows 的命令行中创建一个空文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!