gpt4 book ai didi

windows - 如何在 Windows 的命令行中创建一个空文件?

转载 作者:可可西里 更新时间:2023-11-01 11:50:47 31 4
gpt4 key购买 nike

如何在 DOS/Windows 命令行创建一个空文件?

我试过:

copy nul > file.txt

但是总是显示复制了一个文件。

标准cmd中还有其他方法吗?

应该是不需要touch的方法来自 Cygwin 的命令或任何其他非标准命令。该命令需要从脚本运行,因此不能使用击键。

最佳答案

没有重定向,Luc VuErik 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 :

the shortest one I use is basically the one by Nomad:

.>out.txt

确实报错了:

'.' is not recognized as an internal or external command

但是这个错误是在 stderr 上。和 >只重定向标准输出,其中什么都没有被生成。
因此创建了一个文件。
这里的错误信息可以忽略。或者,如 Rainanswer , 重定向到 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

The "<nul" pipes a nul response to the set/p command, which will cause thevariable used to remain unchanged. As usual with set/p, the string to theright of the equal sign is displayed as a prompt with no 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

The dir command should indicate the file size as 11 bytes: "helloworld!".

关于windows - 如何在 Windows 的命令行中创建一个空文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1702762/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com