如何通过批处理文件在Windows中将文本块(多行文本)写入文件,而不使用多个echo调用?

时间:2019-02-09 19:14:40

标签: batch-file cmd echo

我一直试图在Windows中将多行文本写入文本文件,但是不知道如何执行此操作。另外,我在互联网上进行了大量搜索,但是所有解决方案都多次使用echo命令。

有没有办法像Linux中的“ cat”一样多次使用echo命令呢?

2 个答案:

答案 0 :(得分:0)

more >> filename.txt

应该做你需要的事情

答案 1 :(得分:0)

尽管不是很容易/优雅,但可以用单行回显文本。在使用之前,您需要在store the new line character上添加几行

REM Creating a Newline variable (the two blank lines are required!)
set NLM=^


set NL=^^^%NLM%%NLM%^%NLM%%NLM%
rem just set once, and after that you can use %%NL%% to echo a new line everywhere you want
echo This is a sentence%NL%that's broke down into 2 lines.%NL%And this is another line.

但是您为什么要那样?这将使该行超长,并且没人喜欢水平滚动。也可以通过多次回声调用在同一行中做到这一点

C:\Users>echo This is a very long line of text & echo that requires you to scroll horizontally. & echo And people HATE horizontal scrolls
This is a very long line of text
that requires you to scroll horizontally.
And people HATE horizontal scrolls

所以最简单的方法是使用powershell

C:\Users>powershell -Command "Write-Host This is`na multiline`ntext"
This is
a multiline
text

`n是换行符,因为`是powershell中的转义字符

或者只使用powershell的echo

C:\Users>powershell -Command "echo 'Each parameter to echo' '(A.K.A Write-Output)' 'will be printed on a separate line' 'like this'"
Each parameter to echo
(A.K.A Write-Output)
will be printed on a separate line
like this

如果允许使用多行,但仅使用一个回显,则使用^转义新行并将下一行留空(即,每条输出行2行代码)

(echo Line 1^

Line 2^

Line 3) >file.txt