3

I'm trying to use these command lines in a Windows batch file:

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?

demo7up
  • 444
  • 4
  • 19

1 Answers1

7

Please read my answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? 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:

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

The entire argument string containing reference to environment variable app2 must be usually enclosed in double quotes if being concatenated with other strings. 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. The command ECHO is also an exception on argument string parsing from standard of:

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.

  1. Argument 0 is the IF command itself.
  2. Argument 1 is not.
  3. Argument 2 is 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 &()[]{}^=;!'+,`~.

Well, the string %mypath%\includes\apk\%app2% does not contain a space or parentheses. 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. So the file name with full path processed by IF command contains the parentheses.

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.

The help of Windows command interpreter being cmd.exe interpreting the batch file output on running 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. 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. 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. 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.

Mofi
  • 38,783
  • 14
  • 62
  • 115