如何从批处理文件输出中插入换行符?

我想做的事情是:

echo hello\nworld

这将输出:

hello
world

当前回答

请注意,所有解决方案使用光标定位根据控制台虚拟终端序列,光标定位与:

Sequence Code Description Behaviour
ESC [ <n> E CNL Cursor Next Line Cursor down <n> lines from current position

只有在没有到达控制台窗口底部时才能工作。

在底部没有空间来向下移动光标,所以它只是向左移动(与CRLF的CR),之前打印的行从它的开始被覆盖。

其他回答

要批量地开始新一行,你所要做的就是添加“echo[”,像这样:

echo Hi!
echo[
echo Hello!

在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

日志和屏幕输出看起来像这样…

Echo hello & Echo .world

这意味着您可以定义& echo。作为换行符的常数\n。

请注意,所有解决方案使用光标定位根据控制台虚拟终端序列,光标定位与:

Sequence Code Description Behaviour
ESC [ <n> E CNL Cursor Next Line Cursor down <n> lines from current position

只有在没有到达控制台窗口底部时才能工作。

在底部没有空间来向下移动光标,所以它只是向左移动(与CRLF的CR),之前打印的行从它的开始被覆盖。

如果有人来这里是因为他们想从MINGW make makefile中回显一个空行,我使用

@cmd /c echo。

简单地使用echo。导致可怕的process_begin: CreateProcess(NULL, echo。,…)失败了。错误消息。

我希望这能帮助到至少一个人:)