6

Pretty new to batch scripting. I need the output of a command to be assigned to a variable. I got to know about the FOR /F command. My command has spaces and also double quotes. So I tried using the usebackq option. But it fails with the error on the space.

for /F "usebackq delims=" %%G IN (`"C:\Program Files (x86)\ABC\DEF\XYZ.exe" Get /a "P1=D2" /b "Q1=D3" /c "D5"`) DO (
SET var=%%G
)

ECHO %var%

pause

The failure :

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

As per other questions on StackOverflow, I did everything right - usebackq for the usage of back tick, demlims= to ignore spaces as a delimiter. I am not sure why it is still failing. Any help is appreciated!

4
  • 1
    Is the D in D:\Program actually in the output, or is that a typo and it's really C? Jul 18, 2020 at 19:33
  • UseBackQ is not needed at all in that scenario either, so remove it and revert to the standard ' command quoting style.
    – Compo
    Jul 18, 2020 at 19:59
  • Sorry that was C: - Updated it now. Jul 18, 2020 at 20:42
  • 1
    Perhaps like this: For /F "Delims=" %%G In ('^""%ProgramFiles(x86)%\ABC\DEF\XYZ.exe" Get /a "P1=D2" /b "Q1=D3" /c "D5"^"') Do Set "var=%%G"
    – Compo
    Jul 19, 2020 at 10:14

3 Answers 3

0

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:

  1. ""
  2. C:\Program
  3. Files
  4. (x86
  5. )
  6. \ABC\DEF\XYZ.exe
  7. " Get /a "
  8. P1=D2
  9. " /b "
  10. Q1=D3
  11. " /c "
  12. D5
  13. ""

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.

0

The posted line fails because of the additional quotes around parameters after the program name. One workaround is to embed the entire command line inside another pair of outer quotes (and escape reserved characters like () to ^(^) because they now fall outside a quoted string).

for /F "usebackq delims=" %%G IN (`""C:\Program Files ^(x86^)\ABC\DEF\XYZ.exe" Get /a "P1=D2" /b "Q1=D3" /c "D5""`) do (...)

As noted in a comment, usebackq is not actually needed in this case, and the same syntax will work if using regular quotes, instead.

0

There is no need to use backticks or usebackq here, simply use single quoted string and double quote your paths.

@echo off
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"

Also note that the parenthesized do code block to set the variable is not needed, in fact, as far as I can see, you do not even need the variable as you could get away by just using the metavariable.

@echo off
set "prog=C:\Program Files (x86)\ABC\DEF\XYZ.exe"
for /F "delims=" %%G IN ('"%prog%" Get /a "P1=D2" /b "Q1=D3" /c "D5"') do echo %%G
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.