1

In a batch file I want to set a variable to the one-line output of a program - a task explained in multiple questions and internet ressources.

In my specific task I want to execute the command

"D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release\FileVersion.exe" /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe"

As you can see, the path for the program I want to execute, FileVersion.exe contains spaces, and the second parameter handed to FileVersion.exe contains a full path with spaces as well.

When I call the above command either directly at the command line or by using CALL in the batch file it correctly executes the program prints out the expected output.

Now, to capture the output of executing the program I use the follow batch command

FOR /F "usebackq tokens=* delims=" %%i IN (`"D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release\FileVersion.exe" /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe"`) DO (
    echo %%i
)

The FOR documentation says

usebackq     Use the alternate quoting style:                        
           - Use double quotes for long file names in "filenameset".
           - Use single quotes for 'Text string to process'
           - Use back quotes for `command to process`

I think I did this correctly by using back quotes around the command, and using quotes around the filenames.

However, the call fails with the message (translated from German)

The command "D:\My" is either incorrect or couldn't be found.

I think this points to a problem with escaping the spaces.

The weird part: If I either replace the path of the exe to call, or the parameter with a path sans spaces, it works. So both

FOR /F "usebackq tokens=* delims=" %%i IN (`FileVersion.exe /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe"`) DO (
    echo %%i
)

and

FOR /F "usebackq tokens=* delims=" %%i IN (`"D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release\FileVersion.exe" /n Test.exe`) DO (
    echo %%i
)

work.

Why don't two quote-escaped strings work in the same command and how can I fix it?

Jens
  • 6,275
  • 2
  • 25
  • 51
  • I'm not sure that I see the purpose of using back quotes! – Compo Jun 06 '17 at 11:57
  • 2
    See if [this](https://stackoverflow.com/q/22636308/2861476) solves your problem. – MC ND Jun 06 '17 at 11:58
  • Compo: This doesn't make a difference. @MCND Your's does. I was sure there had to be a resource that answered it but I didn't find it while sifting through the examples. Thanks. I'll mark it as duplicate. – Jens Jun 06 '17 at 12:00
  • 2
    The example seems a little bit more complicated that this question (because of the _caret_ char and also the redirector operator). Translated to what we have here: `for /f "tokens=*" %%f in ('""D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release\FileVersion.exe" /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe""') DO (`... . – CristiFati Jun 06 '17 at 14:15
  • @CristiFati Yes putting the double-quotes around the whole thing, while still adding quotes around each path is the solution I used as well. It was the first example in the accepted answer of the linked question. – Jens Jun 06 '17 at 14:22

1 Answers1

0

I wouldn't use usebackq and first resolve the complex path:

@Echo off
Pushd "D:\__My Program\___My Program\Folder1\Folder2\..\..\FileVersion\FileVersion\bin\Release"
FOR /F "tokens=*" %%i IN (
  ' FileVersion.exe /n "D:\__My Program\___My Program\Folder1\Folder2\bin\Deploy\My Program.exe" '
) DO echo %%i
PopD