拆分Doublequoted线到Windows批量多行 [英] Splitting Doublequoted Line Into Multiple Lines in Windows Batch

查看:129
本文介绍了拆分Doublequoted线到Windows批量多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长命令可以使用​​ ^ ,如提及的<一个被分割为多行href=\"http://stackoverflow.com/questions/69068/long-commands-split-over-multiple-lines-in-vista-dos-batch-bat-file\">this帖子。

Long commands in Windows batch files can be split to multiple lines by using the ^, as mentioned in this post.

但是,如果插入符号是双引号字符串内,它不会工作。例如:

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)建立行参数组合多个。

@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!

在每一行的第一个空间是很重要的,因为插入符号可以作为多行字符,但它也逃脱的第一个字符,所以也报价将被转义。结果
所以我代替unnessary#建设行之后。

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.

修改增加:3)消失报价

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

在我看来,这是的的方式,它的工作原理解析器首先看到的报价,因此最后的光标将工作,因为它似乎是在引号之外。结果
但这!=!前pression将扩展名为变量= ,但是这样的变量名不能存在,因此变成了空(等号只能出现作为第一个字符)。

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 only occur as first character) so it expands to nothing.

您还可以创建的安全的前pressions,他们将永远逃脱出来引号的,独立的,如果有报价或不在行。结果
!^=!

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"
)

这篇关于拆分Doublequoted线到Windows批量多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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