使用多行将字符串回显到 .txt 文件 - 使用 Windows 批处理文件

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

Echo string to .txt file with multiple lines - with Windows Batch file

windowsshellbatch-file

提问by Linuxmint

I am attempting to create a Windows Batch File to create a .txt with mulitple lines. I've tried several solutions to insert a line break in the string but no avail. There are other similar questions/answers but none of them address putting the entire string into a text file.

我正在尝试创建一个 Windows 批处理文件来创建一个多行的 .txt。我尝试了几种解决方案来在字符串中插入换行符,但无济于事。还有其他类似的问题/答案,但没有一个解决将整个字符串放入文本文件的问题。

My batch file currently reads:

我的批处理文件当前显示:

echo Here is my first line
Here is my second line > myNewTextFile.txt
pause

my goal is to have the text file read:

我的目标是让文本文件读取:

Here is my first line
Here is my second line

Obviously, this does not work currently, but wondering if anyone knows how to make this happen in a simple fashion?

显然,这目前不起作用,但想知道是否有人知道如何以简单的方式实现这一点?

回答by Endoro

(
echo Here is my first line
echo Here is my second line
echo Here is my third line
)>"myNewTextFile.txt"
pause

回答by Ken White

Just repeat the echoand >>for lines after the first. >>means that it should append to a file instead of creating a new file (or overwriting an existing file):

只需在第一行之后重复echoand >>for 行。 >>意味着它应该附加到文件而不是创建新文件(或覆盖现有文件):

echo Here is my first line > myNewTextFile.txt
echo Here is my second line >> myNewTextFile.txt
echo Here is my third line >> myNewTextFile.txt
pause

回答by Jos

Searching for something else, I stumbled on this meanwhile old question, and I have an additional little trick that is worth mentioning, I think.

在寻找其他东西时,我偶然发现了这个同时存在的老问题,我认为还有一个值得一提的小技巧。

All solutions have a problem with empty lines and when a line starts with an option for the echo command itself. Compare the output files in these examples:

所有解决方案都存在空行问题,以及当一行以 echo 命令本身的选项开头时。比较这些示例中的输出文件:

call :data1 >file1.txt
call :data2 >file2.txt
exit /b

:data1
echo Next line is empty
echo
echo /? this line starts with /?
echo Last line
exit /b

:data2
echo:Next line is empty
echo:
echo:/? this line starts with /?
echo:Last line
exit /b

Now, file1.txt contains:

现在,file1.txt 包含:

Next line is empty 
ECHO is off. 
Displays messages, or turns command-echoing on or off.

  ECHO [ON | OFF]
  ECHO [message]

Type ECHO without parameters to display the current echo setting. 
Last line

While file2.txt contains:

而 file2.txt 包含:

Next line is empty

/? this line starts with /?
Last line

The use of echo:miraculously solves the issues with the output in file1.txt.

使用echo:奇迹般的解决了file1.txt中输出的问题。

Besides the colon, there are other characters that you could 'paste' to echo, among them a dot, a slash, ... Try for yourself.

除了冒号,您还可以“粘贴”到其他字符echo,其中包括点、斜线、... 自己尝试。