windows 带空格和括号的批处理文件变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15075799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:18:16  来源:igfitidea点击:

Batch file variable with spaces and parentheses

windowsvariablesbatch-fileescaping

提问by Hoobajoob

I've read numerous threads on different approaches to getting the windows batch file parser to properly handle variables that have spaces, parentheses, and other special characters, but none of the recommendations seems to be able to fix the issue I am having.

我已经阅读了许多关于让 Windows 批处理文件解析器正确处理具有空格、括号和其他特殊字符的变量的不同方法的线程,但似乎没有一个建议能够解决我遇到的问题。

Here is the script (prior to trying any workarounds), whose goal is to set a value for variable03 based on the values found for variable01 and variable02:

这是脚本(在尝试任何解决方法之前),其目标是根据为 variable01 和 variable02 找到的值设置 variable03 的值:

set variable01="C:\Program Files (x86)\SomeProgram\Subfolder"
set variable02="${macro}"

set variable01=%variable01:"=%
set variable02=%variable02:"=%

set variable03=""

if %variable02:~0,1%==$ (
   if %variable01:~0,1%==$ (
      set variable03=%variable03:"=%
   ) else (
      set variable03=-o '%variable01%'
   )
)

...the values of variable01 and variable02 are not known in advance - they are substituted by another program prior to running the script, so the above script is showing an example set of values for variable01 and variable02 after that substitution has been made.

...variable01 和 variable02 的值事先未知 - 它们在运行脚本之前被另一个程序替换,因此上面的脚本显示了替换后的 variable01 和 variable02 的一组示例值。

The error I get when this script runs is:

这个脚本运行时我得到的错误是:

\SomeProgram\Subfolder' was unexpected at this time.

...which corresponds to the last 'set' line in the above script. I assumed that this error was due to the parentheses in the value of variable01.

...对应于上述脚本中的最后一个 'set' 行。我认为这个错误是由于 variable01 值中的括号引起的。

If I change that line to this:

如果我将该行更改为:

set "variable03=-o '%variable01%'"

...then I get this error:

...然后我收到此错误:

Files was unexpected at this time.

...which seems to indicate that it is trying to tokenize on the spaces in variable01, and the parser is still not happy.

...这似乎表明它正在尝试对 variable01 中的空格进行标记,但解析器仍然不满意。

If I then add this line at the top of the script:

如果我然后在脚本顶部添加这一行:

 setlocal enableextensions enableDelayedExpansion

...and change %variable01% to !variable01!, I still get the same error.

...并将 %variable01% 更改为 !variable01!,我仍然遇到相同的错误。

Clearly, I do not understand what the batch file parser needs to meet my requirement that the value of variable03 has the following value:

显然,我不明白批处理文件解析器需要什么才能满足我的要求,即 variable03 的值具有以下值:

-o 'C:\Program Files (x86)\SomeProgram\Subfolder'

...any suggestions?

...有什么建议?

采纳答案by Nate Hekman

The problem is the parentheses in variable01's value. Since it's being expanded in an ifcondition, those parentheses are being interpreted as flow control. Fix by always putting it in double quotes.

问题是variable01's 值中的括号。由于它在if条件中被扩展,这些括号被解释为流控制。通过始终将其放在双引号中来修复。

set variable01="C:\Program Files (x86)\SomeProgram\Subfolder"
set variable02="${macro}"

set variable01=%variable01:"=%
set variable02=%variable02:"=%

set variable03=""

if "%variable02:~0,1%"=="$" (
   if "%variable01:~0,1%"=="$" (
      set variable03=%variable03:"=%
   ) else (
      call :set3 "%variable01%"
   )
)
goto :eof

REM If it is important to have single quotes instead of double then
REM I found I had to call a subroutine.  Otherwise the set could be
REM left up in the else.
:set3
set variable03=-o '%~1'
goto :eof

回答by jeb

As Nate wrote, the problem in this case are the brackets, but the complete code is still unstable.

正如 Nate 所写,这种情况下的问题是括号,但完整的代码仍然不稳定。

It's always better to use delayed expansion, as this is safe against any special characters.
And you should use the extended syntax of SETset "variable=content"to enclose the complete expression with quotes, then it's nearly safe, and the quotes are not added to the content.
And you don't need to remove the quotes later.

使用延迟扩展总是更好,因为这对于任何特殊字符都是安全的。
并且您应该使用 的扩展语法SETset "variable=content"将完整的表达式用引号括起来,这样几乎是安全的,并且引号不会添加到内容中。
并且您以后不需要删除引号。

This should work with any content in var1 and var2

这应该适用于 var1 和 var2 中的任何内容

setlocal EnableDelayedExpansion
set "variable01=C:\Program Files (x86)\SomeProgram\Subfolder"
set "variable02=${macro}"

set "variable03="

if "!variable02:~0,1!"=="$" (
   if "!variable01:~0,1!"=="$" (
      rem
   ) else (
      set "variable03=-o '!variable01!'"
   )
)
echo(!variable03!