如何使用批处理文件中if-clause中的括号转义变量? [英] How to escape variables with parentheses inside if-clause in a batch file?

查看:307
本文介绍了如何使用批处理文件中if-clause中的括号转义变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此批处理文件

  @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 :^)= ^^^)!
设置路径

然后,您的路径在 c code code code code code code code $ c $ $ b

要了解扩展的工作原理,您可以阅读 SO:Windows Command Interpreter(CMD.EXE)如何解析脚本?



EDIT2:路径更多的问题(含引号)

根据此问题当路径包含引号时,可能会出现括号中的另一个问题。



示例

path = C:\程序文件(x86); C:\程序文件(x86)\Skype



没有必要在这里使用引号,但这会破坏扩展的 SET synt斧头,如现在设置newPath =%路径%展开为

  setnewPath =C:\Program Files(x86); C:\Program Files(x86)\Skype

现在至少有一个括号不在引号内,可以打破命令块。



但是你可以简单地删除所有的引号路径变量,如上所述,这里不需要引号。

  setnewPath =%path:=%


Running this batch file

@echo off
set a=some value with (parentheses) inside
if 1 == 1 (
    set PATH=%a%
)

gives inside was unexpected at this time. error.

How to escape a variable to avoid this error?

解决方案

You can use two different ways

Use the extended syntax of 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%"
)

Use delayed expansion (like the answer of shf301) but also transfer the value to the main scope.

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

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

@echo off
Setlocal EnableDelayedExpansion
set a=some value with (parentheses) inside
if 1 == 1 (
    set PATH=!a:^)=^^^)!
    set path
)

Then your path contains carets in front of the ) like C:\programs (x86^)

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)
According to this question there can occour another problem with parenthesis when the path contains quotes.

Sample
path="C:\Program Files (x86)";C:\Program Files (x86)\Skype

This is allowed, even it's not necessary to use quotes here, but this destroys the extended SET syntax, as now set "newPath=%path%" expands to

set "newPath="C:\Program Files (x86)";C:\Program Files (x86)\Skype"

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.

set "newPath=%path:"=%"

这篇关于如何使用批处理文件中if-clause中的括号转义变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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