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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1702762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:53:29  来源:igfitidea点击:

How to create an empty file at the command line in Windows?

windowsfilecmdcommand-line

提问by Grendler

How to 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 any other method in the standard cmd?

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

It should be a method that does not require the touchcommand from Cygwin or any other nonstandard commands. The command needs to run from a script so keystrokes cannot be used.

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

回答by VonC

Withoutredirection, Luc Vuor Erik Konstantopoulospointoutto:

如果没有重定向,吕克·武埃里克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


Nomadmentions 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, Samuelsuggests 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. And >only redirects stdout, where nothinghave been produced.
Hence the creation of an emptyfile. The error message can be disregarded here.

但是这个错误是在 stderr 上。并且>只重定向标准输出,没有产生任何东西
因此创建了一个文件。此处可以忽略错误消息。



(Original answer, November 2009)

(原答案,2009 年 11 月)

echo.>filename

(echo ""would actually put "" in the file! And echowithout the '.' would put "Command ECHO activated" in the file...)

echo ""实际上会将 "" 放入文件中!如果echo没有 '.' 会将 " Command ECHO activated" 放入文件中...)

Note: the resulting file is not emptybut includes a return line sequence: 2 bytes.

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



This discussionpoints to a true batch solution for a real emptyfile:

这个讨论指向一个真正的文件的真正批处理解决方案:

 <nul (set/p z=) >filename

 dir filename
 11/09/2009  19:45                 0 filename
 1 file(s)                         0 bytes

The "<nul" pipes a nulresponse to the set/pcommand, 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.

" <nul" 管道nulset/p命令的响应,这将导致使用的变量保持不变。像往常一样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(which is mentioned in Patrick Cuff's answerand 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 dircommand should indicate the file size as 11 bytes: "helloworld!".

dir命令应将文件大小指示为 11 个字节:“ helloworld!”。

回答by Justin

Try this:

尝试这个:

type NUL > 1.txt

this will definitely create an empty file.

这肯定会创建一个空文件。

回答by Patrick Cuff

Here's another way:

这是另一种方式:

cd. > filename

回答by qid

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,复制的输出就会消失。

回答by iCrazybest

Open file :

打开文件 :

type file.txt

New file :

新文件 :

Way 1 : type nul > file.txt
Way 2 : echo This is a sample text file > sample.txt
Way 3 : notepad myfile.txt <press enter>

Edit content:

编辑内容:

notepad file.txt

Copy

复制

copy file1.txt file1Copy.txt

Rename

改名

rename file1.txt file1_rename.txt

Delete file :

删除文件 :

del file.txt

回答by nash

Reading comments on my post, I have to admit I didn't read the question right.

阅读我帖子的评论,我不得不承认我没有正确阅读问题。

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.

此命令更改文件的修改日期或在未找到文件时创建它。

回答by Kris

echo "" > filename

I believe this works on Windows/DOS, but my last hands-on experience with either is quite a while ago. I do know for a fact that it works on basically any POSIXcompliant OS.

我相信这可以在 Windows/DOS 上运行,但我上一次使用这两种方法的经验是很久以前的事了。我确实知道它基本上适用于任何符合POSIX 的操作系统。

回答by nephi12

call>file.txt

this is the cleanest way I know.

这是我所知道的最干净的方式。

回答by Subhashi

cd > filename.cfg 

worked when creating a file in C:/Program Files where you don't have the access to create files directly.

在 C:/Program Files 中创建文件时工作,您无权直接创建文件。

回答by Paul Nathan

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;
}