windows 从批处理文件创建一个空白文件

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

Creating a blank file from a batch file

windowsfilebatch-file

提问by Colin Pickard

I would like create a file from a batch file. I can use the echo. > donald.txt, but it creates it with an initial blank line. What else I can use to a file starting from the first line?

我想从批处理文件创建一个文件。我可以使用echo. > donald.txt,但它使用初始空行创建它。我还可以对从第一行开始的文件使用什么?

回答by Colin Pickard

You just want to create a completely empty file? This will do it:

您只想创建一个完全空的文件?这将做到:

copy /y nul donald.txt

If you're looking to write something to first line, you want something like this:

如果你想在第一行写一些东西,你想要这样的东西:

echo Hello, World > donald.txt

回答by Timo Salmi

One of the options is

选项之一是

@echo off
rem Don't destroy an existing file
if exist testfile goto _nocreate
:: Create the zero-byte file
type nul>testfile
:_nocreate

Originally from http://www.netikka.net/tsneti/info/tscmd041.htmwhich contains some additional methods.

最初来自http://www.netikka.net/tsneti/info/tscmd041.htm,其中包含一些其他方法。

回答by Aacini

In old MS-DOS days I used this method to create an empty file:

在旧的 MS-DOS 时代,我使用这种方法来创建一个空文件:

rem > empty.txt

In new Windows this no longer works, but remmay be replaced by any command that show nothing. For example:

在新的 Windows 中,这不再有效,但rem可以被任何不显示任何命令的命令替换。例如:

cd . > empty.txt

回答by Actorclavilis

If you're using PowerShell:

如果您使用的是 PowerShell:

function touch {set-content -Path ($args[0]) -Value ($null) } 
touch file-name

Source: http://blog.lab49.com/archives/249.

来源:http: //blog.lab49.com/archives/249

回答by Cody Gray

That's because echo.inserts a blank line in your file.

那是因为echo.在您的文件中插入了一个空行。

Try using the same code, but without the period appended to the end of the echostatement:

尝试使用相同的代码,但不要在echo语句末尾附加句点:

echo "Test string" > donald.txt