用空格和括号的批处理文件变量 [英] Batch file variable with spaces and parentheses

查看:182
本文介绍了用空格和括号的批处理文件变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过关于不同的做法多个线程来获取Windows批处理文件分析器妥善处理包含空格,括号和其他特殊字符的变量,但没有建议似乎能解决我遇到的问题

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.

下面是脚本(之前尝试的任何变通办法),其目标是建立发现variable01和variable02基于价值variable03值:

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:

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

... variable01和variable02的值事先不知道 - 它们是由前运行脚本另一个节目取代的,因此,上述脚本示出替代已经经过设定variable01和variable02值的示例的。

...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.

运行此脚本时,我得到的错误是:

The error I get when this script runs is:

\SomeProgram\Subfolder' was unexpected at this time.

...其对应于在上述脚本中的最后设定线。我认为这个错误是由于variable01价值的括号。

...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 "variable03=-o '%variable01%'"

...然后我得到这个错误:

...then I get this error:

Files was unexpected at this time.

...这似乎表明,它正试图记号化在variable01空间,解析器仍然高兴不起来。

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

如果我再在脚本的顶部添加此行:

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

 setlocal enableextensions enableDelayedExpansion

...并更改%variable01%至!variable01!我仍然得到同样的错误。

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

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

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

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

...有什么建议?

...any suggestions?

推荐答案

问题是在 variable01 的价值括号。由于它是在如果状况正在扩大,这些括号是PTED流量控制间$ P $。通过始终将其放在双引号修复。

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

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

这篇关于用空格和括号的批处理文件变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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