简体   繁体   English

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

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

I am attempting to create a Windows Batch File that creates a .txt with mulitple lines.我正在尝试创建一个 Windows 批处理文件,该文件创建一个多行的 .txt。 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.还有其他类似的问题/答案,但没有一个解决将整个字符串放入文本文件的问题。

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?显然,这目前不起作用,但想知道是否有人知道如何以简单的方式实现这一点?

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

Just repeat the echo and >> for lines after the first.只需在第一行之后重复echo>> >> means that it should append to a file instead of creating a new file (or overwriting an existing file): >>表示它应该附加到文件而不是创建新文件(或覆盖现有文件):

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

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.所有解决方案都存在空行问题,以及当一行以 echo 命令本身的选项开头时。 Compare the output files in these examples:比较这些示例中的输出文件:

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 ,其中包括一个点、一个斜杠、...自己尝试。

STEP 1: Enter Line 1 followed by the ^ character.步骤 1:输入第 1 行,后跟 ^ 字符。

echo Here is my first line^ 

STEP 2: Hit RETURN key to get a prompt for more text第 2 步:按 RETURN 键以获取更多文本提示

echo Here is my first line^ 
More?

STEP 3: Hit RETURN key once more to get a second prompt for more text第 3 步:再次点击 RETURN 键以获得更多文本的第二个提示

echo Here is my first line^ 
More?
More?

STEP 4: Continue line 2 from the second prompt第 4 步:从第二个提示继续第 2 行

echo Here is my first line^ 
More?
More? Here is my second line

STEP 5: Hit the RETURN key to get 2 statements displayed on two separate lines第 5 步:按 RETURN 键以在两个单独的行上显示 2 个语句

Results:结果:

echo Here is my first line^ 
More?
More? Here is my second line
Here is my first line  
Here is my second line

NOTE笔记
However, if you wish to save this to file, you could add a final STEP.但是,如果您希望将其保存到文件中,您可以添加一个最终的 STEP。
STEP 6: with the help of the > character, you can append the filename so you save your output to file instead.步骤 6:在 > 字符的帮助下,您可以附加文件名,以便将输出保存到文件中。

echo Here is my first line^ 
More?
More? Here is my second line >"myNewTextFile.txt"

Example from CMD来自 CMD 的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM