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

我想做的事情是:

echo hello\nworld

这将输出:

hello
world

当前回答

Ken和Jeb的解决方案效果很好。

但是新行只生成一个LF字符,我需要CRLF字符(Windows版本)。

为此,在脚本的末尾,我已经将LF转换为CRLF。

例子:

TYPE file.txt | FIND "" /V > file_win.txt
del file.txt
rename file_win.txt file.txt

其他回答

在windows 10的虚拟终端序列中,存在高度控制光标位置的方法。

要定义转义序列0x1b,可以使用以下方法:

@Echo off
 For /f %%a in ('echo prompt $E^| cmd')Do set \E=%%a

在字符串之间输出一个换行符:

<nul set /p "=Hello%\E%[EWorld"

输出n个换行符,其中n被整数替换。

<nul set /p "=%\E%[nE"

Many

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

echo Hi!
echo[
echo Hello!

注意,这不会在控制台工作,因为它将模拟一个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!

在这里,创建一个.bat文件,其中包含以下内容:

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


set NL=^^^%NLM%%NLM%^%NLM%%NLM%
REM Example Usage:
echo There should be a newline%NL%inserted here.

echo.
pause

您应该会看到如下输出:

There should be a newline
inserted here.

Press any key to continue . . .

显然,您只需要REM语句之间的代码。

Use:

echo hello
echo:
echo world