如何通过批处理文件在Windows中将文本块(多行文本)写入文件而不使用多个echo调用? [英] How to write a block of text (multiple lines text) to a file in Windows via batch file without using multiple echo calls?

查看:811
本文介绍了如何通过批处理文件在Windows中将文本块(多行文本)写入文件而不使用多个echo调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to write multiple lines of text to a text file in Windows but don't know how to do this. Also, I've searched on the internet a lot about this but all solutions use echo command multiple times.

有没有办法像Linux中的"cat"一样多次不使用echo命令吗?

Is there any way to do this without using echo command multiple times like "cat" in Linux?

推荐答案

以单行回显文本应该是可能的,尽管这并不容易/优雅.在使用新行字符之前,您还需要几行来存储新行字符.

To echo text with a single line should be possible although not easy/elegant. You'll need a few more lines to store the new line character before using it

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.

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

But why would you want that? That'll make the line super long and nobody likes horizontal scrolling. It's also possible to do that in one line with multiple echo calls

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


So the neatest way is to use powershell

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

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

`n is the newline because ` is the escape character in powershell

或者只使用powershell的 echo

Alternatively just use powershell's 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行代码)


If using multiple lines is allowed, but with just a single echo then escape the new line with ^ and leave the next line blank (i.e. 2 lines of code for each output line)

(echo Line 1^

Line 2^

Line 3) >file.txt

这篇关于如何通过批处理文件在Windows中将文本块(多行文本)写入文件而不使用多个echo调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆