简体   繁体   English

如何在Windows批处理文件中正确转义括号?

[英]How to properly escape parentheses in Windows batch file?

I'm trying to use these command lines in a Windows batch file: 我正在尝试在Windows批处理文件中使用这些命令行:

SET mypath="%programfiles(x86)%\Directory\SubDirectory"
SET app2="com.estrongs.android.pop_4.1.6.8.9.3-604_minAPI14(armeabi^,armeabi-v7a^,x86)(nodpi)_apkmirror.com.apk"

If Not Exist "%mypath%\includes\apk\%app2%" (
  Rem DO SOMETHING
) ELSE (
  Rem DO SOMETHING ELSE
)

But I am getting this error: 但是我收到了这个错误:

(nodpi)_apkmirror.com.apk was unexpected at this time.

What is the reason for this error message on execution of the batch file? 执行批处理文件时出现此错误消息的原因是什么?

Please read my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? 在命令行上使用'set var = text'后,阅读我的答案为什么没有字符串输出'echo%var%'? and you should understand why this works: 你应该明白为什么这样做:

SET "app2=com.estrongs.android.pop_4.1.6.8.9.3-604_minAPI14(armeabi,armeabi-v7a,x86)(nodpi)_apkmirror.com.apk"

Note: The first double quote is left to variable name. 注意:第一个双引号留给变量名。

The command is SET and the argument is: 命令为SET ,参数为:

app2=com.estrongs.android.pop_4.1.6.8.9.3-604_minAPI14(armeabi,armeabi-v7a,x86)(nodpi)_apkmirror.com.apk

which needs to be enclosed in double quotes to get space and these characters &()[]{}^=;!'+,`~<|> interpreted as literal characters. 需要用双引号括起来获取空格和这些字符&()[]{}^=;!'+,`~<|>解释为文字字符。

Don't forget to reference the environment variable app2 enclosed in double quotes on using %app2% or enable delayed expansion and reference variable app2 with !app2! 不要忘了引用环境变量app2使用双引号括起来%app2%或启用延迟扩展和引用变量app2!app2! .

The entire argument string containing reference to environment variable app2 must be usually enclosed in double quotes if being concatenated with other strings. 如果与其他字符串连接,则包含对环境变量app2引用的整个参数字符串通常必须用双引号括起来。 See also How to set environment variables with spaces? 另请参见如何使用空格设置环境变量? Some applications like Windows Explorer require a quoting of file/folder name within an argument string instead of entire argument string. 某些应用程序(如Windows资源管理器)需要在参数字符串中引用文件/文件夹名称,而不是整个参数字符串。 The command ECHO is also an exception on argument string parsing from standard of: 命令ECHO也是参数字符串的一个例外,它从以下标准解析:

Command "argument 1" argument2 "argument 3 with ( and ) in string"
"Path to executable\program.exe" "argument 1" argument2 "argument 3 with ( and ) in string"

Example: 例:

@echo off
set "mypath=%TEMP%"
set "app2=com.estrongs.android.pop_4.1.6.8.9.3-604_minAPI14(armeabi,armeabi-v7a,x86)(nodpi)_apkmirror.com.apk"
if not exist "%mypath%\includes\apk\%app2%" echo File %mypath%\includes\apk\%app2% does not exist!
pause

The IF command has 4 argument strings. IF命令有4个参数字符串。

  1. Argument 0 is the IF command itself. 参数0是IF命令本身。
  2. Argument 1 is not . 参数1 not
  3. Argument 2 is exist . 参数2 exist
  4. Argument 3 is the file name with full path which is %mypath%\\includes\\apk\\%app2% and which must be enclosed in double quotes because of containing a space and one of these characters &()[]{}^=;!'+,`~ . 参数3是具有完整路径的文件名,即%mypath%\\includes\\apk\\%app2% ,由于包含空格和其中一个字符&()[]{}^=;!'+,`~ ,必须用双引号括起来&()[]{}^=;!'+,`~

Well, the string %mypath%\\includes\\apk\\%app2% does not contain a space or parentheses. 好吧,字符串%mypath%\\includes\\apk\\%app2%不包含空格或括号。 But it can be seen on debugging the batch file that Windows command interpreter expands the environment variables during preprocessing of the entire IF command line before executing the command IF . 但是在调试批处理文件时可以看到Windows命令解释程序执行命令IF 之前在整个IF命令行的预处理期间扩展环境变量。 So the file name with full path processed by IF command contains the parentheses. 因此IF命令处理完整路径的文件名包含括号。

Please note that it does not matter what a batch file contains opened in text editor. 请注意,在文本编辑器中打开批处理文件包含的内容无关紧要。 It always matters how the command line or an entire command block starting with ( and ending with matching ) looks like after preprocessing by Windows command interpreter before executing a command line. 在执行命令行之前,通过Windows命令解释程序进行预处理后,命令行或以(以匹配结束)开头的整个命令块的外观总是很重要。

The help of Windows command interpreter being cmd.exe interpreting the batch file output on running cmd /? Windows命令解释器的帮助是cmd.exe在运行cmd /?解释批处理文件输出cmd /? explains in last paragraph on last output help page when a file or folder name must be enclosed in double quotes. 当文件或文件夹名称必须用双引号括起来时,在最后一个输出帮助页面的最后一段中解释。 An argument string containing the redirection operators <|> which should be interpreted as literal characters must be also enclosed in double quotes. 包含应被解释为文字字符的重定向运算符<|>的参数字符串也必须用双引号括起来。 A file/folder name cannot contain <|> . 文件/文件夹名称不能包含<|>

The command lines in batch file as posted in question correct coded: 批处理文件中的命令行发布的问题正确编码:

@echo off
set "mypath=%ProgramFiles(x86)%\MEKNYC\FireMax_Wizard"
set "app2=com.estrongs.android.pop_4.1.6.8.9.3-604_minAPI14(armeabi,armeabi-v7a,x86)(nodpi)_apkmirror.com.apk"

if not exist "%mypath%\includes\apk\%app2%" (
  echo File "%mypath%\includes\apk\%app2%" does not exist.
  rem Other commands on missing file.
) else (
  echo File "%mypath%\includes\apk\%app2%" exists.
  rem Other commands on existing file.
)

On both ECHO command lines the file name with full path is enclosed in double quotes which was not done on previous example. 在两个ECHO命令行中,带有完整路径的文件名用双引号括起来,这在前面的示例中没有完成。 The reason is that %mypath% and %app2% are replaced on both ECHO command lines by the string values of those two environment variables during preprocessing the entire command block starting with ( on IF command line and ending on ) on last command line before the command IF is executed at all. 其原因是, %mypath%%app2%预处理整个命令块开始时被替换上由这两个环境变量的字符串值既ECHO命令行(IF命令行和结束上)之前的最后一个命令行上命令IF完全执行。 So first occurrence of ) in file name not enclosed in double quotes would be interpreted by Windows command interpreter as end of command block of true condition and an error message is the result. 所以,第一次出现的)在没有双引号括起来的文件名会被Windows命令解释器解释为真实状况命令块的结束和错误消息是结果。 The previous example contains an IF condition with just command ECHO to execute without a command block starting with ( and so it is safe to output the entire file name without enclosing it in double quotes. However, I strongly recommend to output file/folder names without/with path by ECHO to console window (not redirected into a file) always enclosed in double quotes because of file/folder string could contain also an ampersand & which causes again problems on being found on a command line outside a double quoted argument string. 前面的示例包含一个IF条件,只有命令ECHO才能在没有命令块的情况下执行(因此可以安全地输出整个文件名而不用双引号括起来。但是,我强烈建议输出文件/文件夹名称而不用/由ECHO到控制台窗口的路径(未重定向到文件中)始终用双引号括起来,因为文件/文件夹字符串也可能包含&符号&这会导致在双引号参数字符串之外的命令行上找到问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM