batch-file - 如何向回显变量添加新行
问题描述
下面的完整代码,如果问题看起来很混乱,对不起。
我正在尝试制作一个将帮助文档添加到指定目录的批处理文件,但该文档需要有新行。问题是,我正在使用 var 保存放入 txt 文档中的文本。
我希望帮助文档看起来像这样:
set help=helper \n new line \n\n more space
看起来像这样:(抱歉,必须使用代码,因为无法正常创建新行)
helper
new line
more space
我已经看到了类似下面示例的内容,但是当我使用它时,文档中的文本只是说 echo 为 ON 或 echo 为 OFF,具体取决于 echo 是否打开(代码在此处找到):
setlocal EnableDelayedExpansion
set LF=^
rem TWO empty lines are required
echo This text!LF!uses two lines
最重要的是,这段代码看起来像是它的新行 var 创建了一个分隔符,而不仅仅是一个新行,就像我上面示例中的“帮助器”和“新行”。我希望“\n”将以下文本放在下一行,并且“\n\n”将文本放在前一个文本之后的两行,例如上面示例中的“更多空间” . 简而言之,我希望“\n”作为新行,“\n\n”作为换行符。
感谢您花时间阅读本文,感谢所有答案和/或提示。如果您对我的意思感到困惑,请随时在评论中问我。我对此很陌生,所以我对某些术语的使用可能很差。除此之外,对于句子和/或语法/标点错误的运行感到抱歉。我只是有点累了,我想我忘记了我的英语 1 课。
我的完整代码是:
@echo off
:start
cls
echo -create
echo -download
echo.
set /p PROGRAM= What do you want to do?:
goto %PROGRAM%
:create
cls
REM saves the text types to the helpDir var
set /p helpDir=Where do you want to save the help file?:
REM makes the text used in the help.txt file, \n would be the new line,
set help=helper \n new line \n\n more space
REM I want to make it look like this:
REM helper
REM new line
REM
REM more space
REM Makes the "help" text to go to a .txt file, then saves the .txt file to the dir specified.
echo %help% > "%helpDir%\help.txt"
pause
goto start
REM not finished yet, please ignore
:download
title Custom Text File
cls
set /p help=Where do you want to save the help doc at?;
set /p txt=Made a command
echo %txt% > "dir
Pause
解决方案
您的第一个样本完美无瑕,但今天更喜欢另一种风格。
setlocal EnableDelayedExpansion
(set LF=^
%=DO NOT REMOVE THIS=%
)
echo This text!LF!uses two lines
如果您在输出中看到空格(而不是换行符),请检查并删除LF
定义中的尾随空格。
根据您的评论,您的问题是百分比扩展。
从您的链接帖子中:
换行最适用于延迟扩展,您也可以将其与百分比扩展一起使用,但它有点复杂。
简单规则:始终扩展包含换行符的变量,仅延迟扩展。
set help=Line1 \n Line2
echo !help!>output.txt
推荐阅读
- python - Keras/TensorFlow GPU——停留在 Epoch 1
- hazelcast - 使用 PER_NODE 策略的 MaxSizeConfig 大小
- matplotlib - matplotlib bar3d 中的可视化错误
- three.js - Threejs围绕与标题垂直的球体旋转
- python - Tensorflow tf.switch_case 不适用于 keras 输入
- python - 将数据从 C 缓冲区传输到 Python,以便在 Visual Studio 2019 中使用 Matplotlib 进行绘图
- postgresql - 不知道为什么我在 PostgreSQL 中得到这个结果
- python - 函数中的for循环不追加数组,而函数外的相同循环确实追加数组
- reactjs - 仅匹配 url 中的特定 id
- node.js - React Native 组件在不同设备之间是不一样的