如何摆脱这种从句里面带括号的变量在批处理文件? [英] How to escape variables with parentheses inside if-clause in a batch file?

查看:597
本文介绍了如何摆脱这种从句里面带括号的变量在批处理文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行这个批处理文件

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

里面在这个时候意外。错误。

如何逃生 A 变量来避免这个错误?

How to escape a variable to avoid this error?

推荐答案

您可以使用两种不同的方式。

You can use two different ways

使用设置用引号扩展语法设置VAR =内容将设置无功与内容,
内容是引述这样的特殊字符都没有问题,而且它使用的内容,直到最后的报价(不带引号本身)

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

使用延​​迟扩展(如shf301的答案),但也值转移到主要适用范围。

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.

编辑:
我可以结合这与SETLOCAL EnableDelayedExpansion和使用!而非%懒惰评估变量的值?当我想我得到了)!意想不到的是在这个时候。

可以,但它是禁忌的生产,如

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
)

那么你的路径包含脱字在像前面
C:\\程序(86 ^)

要了解扩展是如何工作的,你可以读<一个href=\"http://stackoverflow.com/questions/4094699/how-does-the-windows-command-inter$p$pter-cmd-exe-parse-scripts/4095133#4095133\">SO:How不Windows命令国米preTER(CMD.EXE)解析脚本?

To understand how expansion works you can read SO:How does the Windows Command Interpreter (CMD.EXE) parse scripts?

EDIT2:更多的问题与路径(包含引号)结果
根据本<一href=\"http://stackoverflow.com/questions/33116937/call-with-spaces-and-paranthesis-in-path\">question当该路径包含引号可以有occour用括号另一个问题。

More problems with the path (containing quotes)
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 语法,现在设置的新路径=%路径%扩展到

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:"=%"

这篇关于如何摆脱这种从句里面带括号的变量在批处理文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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