如何使用批处理文件中if-clause中的括号转义变量? [英] How to escape variables with parentheses inside if-clause in a batch file?
问题描述
运行此批处理文件
@echo off
设置a =某些值(括号内)
如果1 == 1(
设置PATH =%a%
)
给里面是意外的。
错误
如何逃避 a
变量以避免此错误
您可以使用两种不同的方式
使用设置
的扩展语法与引号设置var = content
将设置var与内容,
内容被引用,所以特殊字符没有问题,它使用的内容,直到最后一个报价(没有报价本身)
@echo off
设置a =一些值(括号内)
如果1 == 1(
设置PATH =%a%
)
使用延迟扩展(如shf301的答案),但也将该值转移到主范围。
@echo off
setlocal en abledelayedexpansion
设置a =某些值($括号内)
如果1 == 1(
设置localScope_PATH =!a!
rem现在将其传输到全局范围
FOR / Fdelims =%% A in(!localScope_PATH!)DO(
endlocal
setpath = %% A
)
)
在这种情况下,扩展集语法不是必需的,我仅使用它来避免隐藏的空格
编辑:
我可以将其与setlocal EnableDelayedExpansion结合使用!而不是%懒惰评估变量的值?当我试过我得到)!在这个时候是意想不到的。
你可以,但它是有争议的,因为
@echo off
Setlocal EnableDelayedExpansion
设置a =一些值(括号内)
如果1 == 1(
设置PATH =!a :^)= ^^^)!
设置路径
)
然后,您的路径在 要了解扩展的工作原理,您可以阅读 SO:Windows Command Interpreter(CMD.EXE)如何解析脚本? EDIT2:路径更多的问题(含引号) 示例 没有必要在这里使用引号,但这会破坏扩展的 现在至少有一个括号不在引号内,可以打破命令块。 但是你可以简单地删除所有的引号路径变量,如上所述,这里不需要引号。 Running this batch file gives How to escape You can use two different ways Use the extended syntax of Use delayed expansion (like the answer of shf301) but also transfer the value to the main scope. In this case the extended set-syntax is not necessary, I used it only to avoid hidden spaces at the line end. EDIT:
Can I combine this with setlocal EnableDelayedExpansion and using ! instead of % to lazy evaluate variable's value? When I tried I got )! was unexpected at this time. You can, but it's contra productive, as Then your path contains carets in front of the To understand how expansion works you can read SO:How does the Windows Command Interpreter (CMD.EXE) parse scripts? EDIT2: More problems with the path (containing quotes) Sample This is allowed, even it's not necessary to use quotes here, but this destroys the extended Now at least one parenthesis is not inside quotes and is able to break a command block. But you can simply remove all quotes from the path variable, as said, quotes aren't necessary here.
这篇关于如何使用批处理文件中if-clause中的括号转义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! c code code code code code code code $ c $ $ b
根据此问题当路径包含引号时,可能会出现括号中的另一个问题。
path = C:\程序文件(x86); C:\程序文件(x86)\Skype
SET
synt斧头,如现在设置newPath =%路径%
展开为
setnewPath =C:\Program Files(x86); C:\Program Files(x86)\Skype
setnewPath =%path:=%
@echo off
set a=some value with (parentheses) inside
if 1 == 1 (
set PATH=%a%
)
inside was unexpected at this time.
error.a
variable to avoid this error?set
with quotes set "var=content"
will set var with content,
content is quoted so special characters aren't problematic and it uses the content till the last quote (without the quote itself)@echo off
set a=some value with (parentheses) inside
if 1 == 1 (
set "PATH=%a%"
)
@echo off
setlocal enabledelayedexpansion
set a=some value with (parentheses) inside
if 1 == 1 (
set "localScope_PATH=!a!"
rem now transfer it to the global scope
FOR /F "delims=" %%A in ("!localScope_PATH!") DO (
endlocal
set "path=%%A"
)
)
@echo off
Setlocal EnableDelayedExpansion
set a=some value with (parentheses) inside
if 1 == 1 (
set PATH=!a:^)=^^^)!
set path
)
)
like
C:\programs (x86^)
According to this question there can occour another problem with parenthesis when the path contains quotes.
path="C:\Program Files (x86)";C:\Program Files (x86)\Skype
SET
syntax, as now set "newPath=%path%"
expands to set "newPath="C:\Program Files (x86)";C:\Program Files (x86)\Skype"
set "newPath=%path:"=%"