The problem has to do with how FOR /F executes the command. You can test via the following script:
Code: Select all
@echo off
for /f "delims=" %%A in ('echo %%cmdcmdline%%') do echo %%A
--OUTPUT--
Code: Select all
C:\Windows\system32\cmd.exe /c echo %cmdcmdline%
So now plug your command into the mix, and it attempts to execute:
Code: Select all
C:\Windows\system32\cmd.exe /c "C:\some path\myprog" -someparameter "some file"
You might think that is OK, but if fails because of item 2. from the following extract of CMD /?
Code: Select all
If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:
1. If all of the following conditions are met, then quote characters
on the command line are preserved:
- no /S switch
- exactly two quote characters
- no special characters between the two quote characters,
where special is one of: &<>()@^|
- there are one or more whitespace characters between the
two quote characters
- the string between the two quote characters is the name
of an executable file.
2. Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
The first and last quotes are stripped, so the command becomes:
Code: Select all
C:\some path\myprog" -someparameter "some file
The solution is pretty simple: Just add an extra pair of enclosing escaped quotes:Code: Select all
FOR /F %%A IN ('^""%mypath%\myprog" -someparameter "%myfile%"^"') DO SET %%A
Dave Benham