如何在Windows批处理文件中将双引号分隔成多行? [英] How to split double quoted line into multiple lines in Windows batch file?

查看:133
本文介绍了如何在Windows批处理文件中将双引号分隔成多行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将长批处理命令分成多行,可以使用^将Windows批处理文件中的长命令分成多行. Windows Vista批处理(.bat)文件.

但是,如果插入符号位于双引号引起来的字符串中,则它将不起作用.例如:

echo "A very long line I want to ^
split into two lines"

这将打印"A very long line I want to ^并告诉我split是未知命令.

有没有办法解决这个问题?

解决方案

我看到了三种可能的解决方法.

1)构建包含多个参数的线.

@echo off
SETLOCAL EnableDelayedExpansion

set "line="
for %%a in ("line1" 
"line2"
"line3"
"line4"
) do set line=!line!%%~a
echo !line!

缺点:当文本中有?*时,它会掉线.

2)在每行末尾加上报价"

@echo on
SETLOCAL EnableDelayedExpansion

set "line=line1 & x#"^
 "line2 & a#"^
 "line3 & b #"^
 "line4 & c "

set "line=!line:#" "=!"
echo !line!

每行的第一个空格很重要,因为插入符号用作多行字符,但它也转义了第一个字符,因此也转义了引号.
因此,在建立该行之后,我替换了不必要的#".

编辑已添加:3)引号消失

setlocal EnableDelayedExpansion
echo "A very long line I want to !="!^
split into two lines"

在我看来,这是最佳的方式,它在解析器首先看到引号时起作用,因此最后的插入号将起作用,因为它似乎不在引号之内.
但是,此!="!表达式将扩展名为="的变量,但是这样的变量名称不存在(第一个字符不能出现等号),因此它不能扩展为任何字符.

您还可以创建 safe 表达式,它们始终会跳出引号,而无论行中是否有引号,它们都是独立的.
!="^"!

echo This multiline works !="^"!^
as expected
echo "This multiline works !="^"!^
too"

如果要避免延迟扩展,还可以使用-FOR-Loop之类的

for %%^" in ("") do (
echo "This multiline works %%~"^
too"
)

Long commands in Windows batch files can be split to multiple lines by using the ^ as mentioned in Long commands split over multiple lines in Windows Vista batch (.bat) file.

However, if the caret is inside a double quoted string, it won't work. For example:

echo "A very long line I want to ^
split into two lines"

This will print "A very long line I want to ^ and tell me split is an unknown command.

Is there a way to get around this?

解决方案

I see three possible workarounds.

1) Building the line combining multiple for-parameters.

@echo off
SETLOCAL EnableDelayedExpansion

set "line="
for %%a in ("line1" 
"line2"
"line3"
"line4"
) do set line=!line!%%~a
echo !line!

Drawback: It drops lines, when there is a ? or * in the text.

2) Leaving the "quote" at the end of each line

@echo on
SETLOCAL EnableDelayedExpansion

set "line=line1 & x#"^
 "line2 & a#"^
 "line3 & b #"^
 "line4 & c "

set "line=!line:#" "=!"
echo !line!

The first space in each line is important, because the caret works as multiline character but it also escapes the first character, so also a quote would be escaped.
So I replace the unnessary #" " after building the line.

EDIT Added: 3) Disappearing quotes

setlocal EnableDelayedExpansion
echo "A very long line I want to !="!^
split into two lines"

In my opinion this is the best way, it works as the parser first see the quotes and therefore the last caret will work, as it seems to be outside of the quotes.
But this !="! expression will expand the variable named =", but such a variable name can't exists (an equal sign can't occur as first character) so it expands to nothing.

You can also create safe expressions, they will always escape out of quotes, independent if there is a quote or not in the line.
!="^"!

echo This multiline works !="^"!^
as expected
echo "This multiline works !="^"!^
too"

If you want avoid delayed expansion, you could also use a -FOR-Loop like

for %%^" in ("") do (
echo "This multiline works %%~"^
too"
)

这篇关于如何在Windows批处理文件中将双引号分隔成多行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆