windows 在批处理文件中创建文件命令 (*.bat)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2046502/
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-15 13:44:15  来源:igfitidea点击:

Create File Command in Batch Files (*.bat)

windowsbatch-file

提问by Graviton

I want to create a file named "new text document.txt" in the folder %tv%using a batch file ( *.bat). This is my batch file:

我想%tv%使用批处理文件 ( *.bat)在文件夹中创建一个名为“新文本文档.txt”的文件。这是我的批处理文件:

set tv=D:\prog\arpack96\ARPACK\SRC
cd "%tv%"
@CON >> "new text document.txt"
set tv=

Although I can really create the file in %tv%, but when I run the above batch file, I will get an error message saying

虽然我真的可以在%tv%.

' ' is not recognized as an internal or external command, operable program or batch file.

“ ”不是内部或外部命令,也不是可运行的程序或批处理文件。

Is there anyway to get rid of this error message? Or I am creating the file using the wrong command?

有没有办法摆脱这个错误信息?或者我正在使用错误的命令创建文件?

回答by paxdiablo

In order to get a truly empty file without having to provide user interaction, you can use the set /pcommand with a little trickery:

为了在无需提供用户交互的情况下获得一个真正的空文件,您可以使用该set /p命令稍微有点技巧:

set tv=c:\documents and settings\administrator
cd "%tv%"
<nul >"new text document.txt" (set /p tv=)

The set /pasks the user for input into the tvvariable after outputting the prompt after the =character.

set /p要求用户输入到tv所述后输出该提示后变量=字符。

Since the prompt is empty, no prompt is shown. Since you're reading from nul, it doesn't wait for user interaction. And you can store the empty string straight into tvthus removing the need to unset it.

由于提示为空,所以不显示提示。由于您正在从 nul 读取,因此它不会等待用户交互。您可以将空字符串直接存储到其中,tv从而无需取消设置。



Actually, after some more thought, there's an easier way. I've used that set /ptrick in the past since it's the only way I know of echoing text without a newline being added (great for progress bars in the console). But if all you want is an empty file, you can probably get away with:

其实,经过深思熟虑,还有一个更简单的方法。我过去使用过这个set /p技巧,因为这是我知道的唯一一种无需添加换行符即可回显文本的方法(非常适合控制台中的进度条)。但是如果你想要的只是一个空文件,你可能会逃脱:

copy /y nul "new text document.txt"

The copysimply copies the contents of the nuldevice (effectively an empty file) to your new file name. The /yis to ensure it's overwritten without bothering the user.

copy简单地复制的内容nul设备(有效的空文件)到新的文件名。这/y是为了确保在不打扰用户的情况下覆盖它。

回答by Ravindra

Create an empty file from a *.bat file - this worked for me.

从 *.bat 文件创建一个空文件 - 这对我有用。

echo off > test.txt

回答by jspcal

type nul > file.txt

file.txt should be created empty

file.txt 应创建为空

or paxdiablo's answer

paxdiablo的答案

回答by Graviton

con is the windows specialized file name and it should not be used.

con 是 Windows 专用文件名,不应使用它。

copy con >> filename.txt

复制 con >> 文件名.txt

shall ask you to enter the text and you can save the text by typing Ctrl Z.

将要求您输入文本,您可以通过键入 Ctrl Z 来保存文本。