如何在Windows的命令行中创建一个空文件? [英] How to create an empty file at the command line in Windows?

查看:847
本文介绍了如何在Windows的命令行中创建一个空文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我试过:

  copy nul> file.txt 

但它总是显示文件已复制。



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



它应该是一个不需要 touch 命令从Cygwin或任何其他非标准命令。
命令需要从脚本运行,所以不能使用击键。

解决方案

>重定向, Luc Vu Erik Konstantopoulos 到:

  copy NUL EMptyFile.txt 
copy / b NUL EmptyFile .txt






如何从批处理文件创建空文本文件?(2008)也指向:

  type NUL> EmptyFile.txt 
#also
echo。 2> EmptyFile.txt
copy nul file.txt> nul#也在qid的回答低于
REM。 > empty.file
fsutil文件createnew file.cmd 0#在映射驱动器上创建文件



< hr>

Nomad 提及原始邮件

  C:\Users\VonC\\ \\ prog \tests> aaaa> empty_file 
'aaaa'不被识别为内部或外部命令,可操作的程序或批处理文件。

C:\Users\VonC\prog\tests> dir

文件夹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






(原答案,2009年11月)

  echo。> filename 

echo会将放在文件中! c $ c> echo 没有'。'会在文件中输入命令ECHO激活



注意:生成的文件不是空的,但包含一个返回行序列:2个字节。






讨论指向真实的空文件的真实批处理解决方案:

 < nul(set / pz =)> filename 

dir filename
11/09/2009 19:45 0 filename
1个文件0 bytes




< nul 管道a nul 响应 set / p 命令,这将导致
变量使用保持不变。和 set / p 一样,等号右边的
字符串显示为没有CRLF的提示符。


由于这里的等号右边的字符串为空...结果是一个空文件。 p>




cd的区别。 > filename (在 Patrick Cuff的回答,并且也产生一个0字节长度的文件)是这个重定向位( trick)可用于没有任何CR 的回显线:

 < nul set / pz = hello)> out.txt 
< nul(set / pz = world!)>> out.txt
dir out.txt




dir命令应该指示文件大小为12个字节: hello world! / code>。



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.

解决方案

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


(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!".

这篇关于如何在Windows的命令行中创建一个空文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆