A command line within '
or within `
on using usebackq
is executed by FOR with option /F
respectively the Windows command process cmd.exe
processing the batch file in background with %ComSpec% /c
and the command line appended as additional arguments.
ComSpec
is a Windows environment variable predefined with %SystemRoot%\System32\cmd.exe
as system environment variable.
The help output on running cmd /?
in a command prompt window explains how the strings are interpreted by Windows command processor after option /C
(execute command line and close) or option /K
(execute command line and keep running).
The usage of backquotes is not needed here as the command line to execute does not contain itself one or more arguments with '
. So it is possible to enclose the command line in '
.
It must be taken into account for the command line to execute by FOR, how the Windows command interpreter parses a batch script, i.e. how the FOR command line looks after parsing and processing it by cmd.exe
processing the batch file which can be seen on debugging the batch file and how the command line is passed as argument string(s) to the second cmd.exe
instance executed in background.
There are at least following two solutions.
for /F "delims=" %%G in ('^""%ProgramFiles(x86)%\ABC\DEF\XYZ.exe" Get /a "P1=D2" /b "Q1=D3" /c "D5"^"') do set "var=%%G"
for /F "delims=" %%G in ('""C:\Program Files (x86^)\ABC\DEF\XYZ.exe" Get /a "P1^=D2" /b "Q1^=D3" /c "D5""') do set "var=%%G"
The first solution was suggested by Compo and is the easier one. The entire command line is enclosed in "
which cmd.exe
started in background removes before processing and executing the remaining command line. The additionally added "
at beginning and at end of the command line are escaped with ^
for cmd.exe
processing the batch file to be interpreted as literal character and not as beginning/end of an argument string. So the argument strings between the two escaped "
are interpreted normally by cmd.exe
processing the FOR command line.
The second solution is more difficult to understand. The entire command line is also enclosed in "
for cmd.exe
started in background. But the double quotes are not escaped for cmd.exe
processing the batch file and for that reason this cmd.exe
instance interprets the command line now as list of following argument strings:
""
C:\Program
Files
(x86
)
\ABC\DEF\XYZ.exe
" Get /a "
P1=D2
" /b "
Q1=D3
" /c "
D5
""
The closing round bracket after (x86
is not interpreted as literal character in this case because of being outside a double quoted argument string for cmd.exe
processing the entire FOR command line. For that reason it must be escaped with ^
to be interpreted as literal character and not as end of set placed at wrong position before '
marking end of the command line to execute.
The two equal signs are both outside a double quoted argument string for cmd.exe
processing the batch file. Therefore cmd.exe
would interpret them as argument separator and would replace each =
by a space character. That is not wanted here because of the arguments should be passed as P1=D2
and Q1=D3
to the executable and not as P1 D2
and Q1 D3
. So both equal signs must be escaped with ^
to be interpreted as literal character and not as argument separator by cmd.exe
processing the batch file.
Both solutions result in running in background with Windows installed into C:\Windows
and 32-bit program files into C:\Program Files (x86)
:
C:\Windows\System32\cmd.exe /c ""C:\Program Files (x86)\ABC\DEF\XYZ.exe" Get /a "P1=D2" /b "Q1=D3" /c "D5""
The background command process removes the first and last "
and then runs
"C:\Program Files (x86)\ABC\DEF\XYZ.exe" Get /a "P1=D2" /b "Q1=D3" /c "D5"
It is possible to create in directory C:\Program Files (x86)\ABC\DEF
a batch file XYZ.bat
containing just the command line @echo %0 %*
and replace .exe
by .bat
in the batch file containing the FOR command line to see if the batch file is executed at all and with which parameters passed to the batch file. This helps to understand what is going on with removing @echo off
and running the batch file with the FOR command line from within a command prompt window.