如何从批处理文件输出中插入换行符?
我想做的事情是:
echo hello\nworld
这将输出:
hello
world
如何从批处理文件输出中插入换行符?
我想做的事情是:
echo hello\nworld
这将输出:
hello
world
当前回答
在Ken的回答中添加一个变量,它显示了环境变量的设置值,其中包含新行。
我们使用此方法将错误条件附加到VAR中的字符串中,然后在所有错误检查输出的末尾将所有错误汇总到文件中。
这不是完整的代码,只是一个示例。
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
:: the two blank lines are required!
set NLM=^
set NL=^^^%NLM%%NLM%^%NLM%%NLM%
:: Example Usage:
Set ErrMsg=Start Reporting:
:: some logic here finds an error condition and appends the error report
set ErrMsg=!ErrMsg!!NL!Error Title1!NL!Description!NL!Summary!NL!
:: some logic here finds another error condition and appends the error report
set ErrMsg=!ErrMsg!!NL!Error Title2!NL!Description!NL!Summary!NL!
:: some logic here finds another error condition and appends the error report
set ErrMsg=!ErrMsg!!NL!Error Title3!NL!Description!NL!Summary!NL!
echo %ErrMsg%
pause
echo %ErrMsg% > MyLogFile.log
日志和屏幕输出看起来像这样…
其他回答
如果需要在可以传递给变量的字符串中使用著名的\n,可以编写如下Hello.bat脚本所示的代码:
@echo off
set input=%1
if defined input (
set answer=Hi!\nWhy did you call me a %input%?
) else (
set answer=Hi!\nHow are you?\nWe are friends, you know?\nYou can call me by name.
)
setlocal enableDelayedExpansion
set newline=^
rem Two empty lines above are essential
echo %answer:\n=!newline!%
通过这种方式,多行输出可以在一个地方准备,甚至在其他脚本或外部文件中,并在另一个地方打印。
The line break is held in newline variable. Its value must be substituted after the echo line is expanded so I use setlocal enableDelayedExpansion to enable exclamation signs which expand variables on execution. And the execution substitutes \n with newline contents (look for syntax at help set). We could of course use !newline! while setting the answer but \n is more convenient. It may be passed from outside (try Hello R2\nD2), where nobody knows the name of variable holding the line break (Yes, Hello C3!newline!P0 works the same way).
上面的例子可以细化为子程序或独立批处理,如调用:mlecho Hi\ i 'm your computer:
:mlecho
setlocal enableDelayedExpansion
set text=%*
set nl=^
echo %text:\n=!nl!%
goto:eof
请注意,额外的反斜杠不会阻止脚本解析\n子字符串。
如果有人来这里是因为他们想从MINGW make makefile中回显一个空行,我使用
@cmd /c echo。
简单地使用echo。导致可怕的process_begin: CreateProcess(NULL, echo。,…)失败了。错误消息。
我希望这能帮助到至少一个人:)
注意,这不会在控制台工作,因为它将模拟一个escape键和清除行。
使用这段代码,将<ESC>替换为0x1b转义字符或使用这个Pastebin链接:
:: Replace <ESC> with the 0x1b escape character or copy from this Pastebin:
:: https://pastebin.com/xLWKTQZQ
echo Hello<ESC>[Eworld!
:: OR
set "\n=<ESC>[E"
echo Hello%\n%world!
就像肯的答案一样,只是用了延迟扩展。
setlocal EnableDelayedExpansion
(set \n=^
%=Do not remove this line=%
)
echo Line1!\n!Line2
echo Works also with quotes "!\n!line2"
First a single linefeed character is created and assigned to the \n-variable. This works as the caret at the line end tries to escape the next character, but if this is a Linefeed it is ignored and the next character is read and escaped (even if this is also a linefeed). Then you need a third linefeed to end the current instruction, else the third line would be appended to the LF-variable. Even batch files have line endings with CR/LF only the LF are important, as the CR's are removed in this phase of the parser.
使用延迟展开的优点是,根本不需要进行特殊的字符处理。 Line1%LF%Line2将失败,因为解析器在单个换行处停止解析。
更多解释见 SO:在Vista/DOS批处理(.bat)文件中,长命令拆分为多行 那么:Windows命令解释器(CMD.EXE)如何解析脚本?
编辑:避免回声。
这并没有回答问题,因为问题是关于单个echo可以输出多行。
但是除了其他答案谁建议使用回声。要创建新行,应注意echo。是最糟糕的,因为它非常慢,它可以完全失败,因为cmd.exe搜索一个名为ECHO的文件,并试图启动它。
如果只打印空行,可以使用其中之一
echo,
echo;
echo(
echo/
echo+
echo=
而是回声的使用。, echo\或echo:应该避免,因为它们会非常慢,这取决于脚本执行的位置,比如网络驱动器。
这对我来说很有效,不需要延迟扩展:
@echo off
(
echo ^<html^>
echo ^<body^>
echo Hello
echo ^</body^>
echo ^</html^>
)
pause
它像这样写输出:
<html>
<body>
Hello
</body>
</html>
Press any key to continue . . .