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

问题描述 投票:-2回答:2

我一直在尝试将多行文本写入Windows中的文本文件,但不知道如何执行此操作。此外,我在互联网上搜索了很多关于此的内容,但所有解决方案都多次使用echo命令。

如果没有像Linux中的“cat”那样多次使用echo命令,有没有办法做到这一点?

batch-file cmd echo
2个回答
0
投票
more >> filename.txt

应该做你需要的


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
© www.soinside.com 2019 - 2024. All rights reserved.