windows 批处理文件是否支持多行变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3294599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do Batch files support multiline variables
提问by Byron Whitlock
If so How?
如果是这样怎么办?
Yes, batch files are lame, but I cannot use powershell, and I don't feel like writing a real app to do this simple task....
是的,批处理文件很蹩脚,但我不能使用 powershell,而且我不想编写一个真正的应用程序来完成这个简单的任务......
edit
编辑
What i want is somthing along the lines of
我想要的是类似的东西
set var="this is a
multi
line
string "
回答by jeb
Or you can create a "real" newline character.
或者您可以创建一个“真正的”换行符。
setlocal enableDelayedExpansion
set NL=^
rem two empty line required
echo first line !NL! second line
set multi=Line1!NL!Line2
set multi=!multi!!NL!Line3
echo !Multi!
With this variant the newline is a "normal" character in the string, so the variables act normally and you can assign them to another variable, this is not possible with the &echo.
trick (which is useful for simple tasks).
对于这个变体,换行符是字符串中的“正常”字符,因此变量正常运行,您可以将它们分配给另一个变量,这是不可能的&echo.
(这对于简单任务很有用)。
回答by T1000
Is that ok?
这可以吗?
@echo off
set var=kur
set var2=kur2
echo var is = "%var%" and var2 is = %var2%
edit
is that what you mean ?
编辑
是你的意思吗?
@echo off
set nl=^& echo.
echo this%nl%is%nl%multiline%nl%string
回答by Fabricio
SET myFlags= ^
a ^
b ^
c
回答by Tom
This is a pretty old question, but I put together a hybrid of the solutions from @Fabricio and @jeb that both worked correctly and added some readability:
这是一个很老的问题,但我将@Fabricio 和@jeb 的解决方案混合在一起,它们都可以正常工作并增加了一些可读性:
setlocal enableDelayedExpansion
set NL=^
rem two empty line required
set var=this is a !NL! ^
multi !NL! ^
line !NL! ^
string !NL!
echo !var!
回答by Arnold Lai
If you just wanted to use it for outputting the lines of variables, I hope this would be useful to you.
如果你只是想用它来输出变量行,我希望这对你有用。
Inspired by something, I found this working.
受到某些东西的启发,我发现这很有效。
For codes in normal Command Prompt:
set this=echo hi ^& echo bye
%this%
对于普通命令提示符中的代码:
set this=echo hi ^& echo bye
%this%
It gives out the two following two lines:
它给出了以下两行:
hi
bye
in the Command Prompt window.
在命令提示符窗口中。
The reason for using the ^
character:
使用^
字符的原因:
Command Prompt would identify that you wanted to run two commands so that you need to add this character, that tells Command Prompt to recognize the next character as a symbol and not to do any special thing for it.
命令提示符会识别您要运行两个命令,因此您需要添加此字符,这会告诉命令提示符将下一个字符识别为符号,而不是为它做任何特殊的事情。
回答by AlexL
And now - without auxiliary SET commands:
现在 - 没有辅助 SET 命令:
echo this is a & echo multi & echo line & echo string
回答by SwiftNinjaPro
I think I figured it out. I had to add ^
to the %NL%
var when putting it in the string, otherwise it hides the text after the first %NL%
because its executing when being set, instead of when its being echoed later
我想我想通了。我必须在将它放入字符串时添加^
到%NL%
var 中,否则它会在第一个之后隐藏文本,%NL%
因为它在设置时执行,而不是在稍后回显时
set NL=^& echo
set str=text on line 1 ^%NL% text on line 2
echo %str%
text on line 1
text on line 2
回答by Arnold Lai
If you wanted to build something instead, not just outputting, hope this would be useful to you too.
如果你想构建一些东西,而不仅仅是输出,希望这对你也有用。
Firstly, you should make a file. Such as .txt
. I'm going to use var.txt
as an example.
首先,你应该制作一个文件。比如.txt
。我将以此var.txt
为例。
Secondly, type in the multi-lines of variables.
其次,输入多行变量。
Thirdly, here are the codes, but only works for batch:
@echo off set num=0 set /p this%num%=<"var.txt" goto there :there set /a num=num+1 for /f "skip=%num%" %%a in (var.txt) do (set this%num% =%%a goto there) set num=0 goto build :build rem Example of using multi-lines of variable. call echo %%this%num% %% set /a num=num+1 for /f "skip=%num% delims=" %%a in (var.txt) do (call echo %%this%num% %% goto build) rem Just replace the commands with `echo` part into something else which suits your situation. pause >nul exit
第三,这里是代码,但仅适用于批处理:
@echo off set num=0 set /p this%num%=<"var.txt" goto there :there set /a num=num+1 for /f "skip=%num%" %%a in (var.txt) do (set this%num% =%%a goto there) set num=0 goto build :build rem Example of using multi-lines of variable. call echo %%this%num% %% set /a num=num+1 for /f "skip=%num% delims=" %%a in (var.txt) do (call echo %%this%num% %% goto build) rem Just replace the commands with `echo` part into something else which suits your situation. pause >nul exit
I admit that strictly speaking, this is not a variable of multi-line, but in my opinion, this is better than nothing.
我承认,严格来说,这不是多行的变量,但在我看来,这总比没有好。
You may ask why not just use the setlocal EnableDelayedExpansion
command. The reason is simply, that command does work for me (IDK why). Therefore, I thought and made these codes that make the same effect.
您可能会问为什么不直接使用setlocal EnableDelayedExpansion
命令。原因很简单,该命令对我有用(IDK 为什么)。因此,我想到并制作了这些具有相同效果的代码。
回答by Blackkatt
I prefer this method
我更喜欢这种方法
@ECHO OFF
SETLOCAL EnableDelayedExpansion
REM (define NewLine character)
(SET _NL=^
%=this line is empty=%
)