没有重定向,Luc Vu 或 Erik Konstantopoulos point out 到:
copy NUL EMptyFile.txt
copy /b NUL EmptyFile.txt
"How to create empty text file from a batch file?" (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 提到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
本着同样的精神,Samuel 建议 in the comments:
我用的最短的基本上是Nomad的那个:
.>out.txt
它确实给出了一个错误:
'.' is not recognized as an internal or external command
但是这个错误在标准错误上。而> 仅重定向标准输出,其中什么都没有产生。
因此创建了一个 empty 文件。
此处可以忽略错误消息。或者,如Rain的answer,重定向到NUL:
.>out.txt 2>NUL
(原始答案,2009 年 11 月)
echo.>filename
(echo "" 实际上会将“”放入文件中!而没有“.”的echo 会将“Command ECHO activated”放入文件中...)
注意:生成的文件不是空,而是包含一个返回行序列:2 个字节。
这个discussion 指向一个真正的空文件的真正批处理解决方案:
<nul (set/p z=) >filename
dir filename
11/09/2009 19:45 0 filename
1 file(s) 0 bytes
“<nul”通过管道将nul 响应传递给set/p 命令,这将导致
变量过去保持不变。像往常一样使用set/p,字符串
等号右侧显示为没有 CRLF 的提示。
因为这里“等号右边的字符串”是空的……结果是一个空文件。
与cd. > filename 的区别(Patrick Cuff's answer 中提到并且也生成一个 0 字节长度的文件)是这个“重定向位”(<nul... 技巧)可以用于 没有任何 CR 的回显线:
<nul (set/p z=hello) >out.txt
<nul (set/p z= world!) >>out.txt
dir out.txt
dir 命令应将文件大小指示为 11 个字节:“helloworld!”。