Page 1 of 1

For /f Quotes - Run a program with a long file name.

Posted: 2021-Aug-23, 8:12 am
by darwin4ever
Hi,

I have a program which accepts a string (may have spaces when surrounded by double quotes) as parameter and returns a string (may have spaces)

Like this :
"My Program Name.exe" "this is a string"
and the output might be "string a is this"
and that works

But if I want to capture the return-string in a variable with a
for /f %%i in ('"My Program Name.exe" "this is a string"') do set OutputString=%%i
I can't get it working

Tried several things :
for /f %%i in ('"My Program Name.exe"') do set OutputString=%%i --> program executes but does nothing as no given parameter

for /f %%i in ('"My Program Name.exe" "this is a string"') do set OutputString=%%i --> "My" is not recognized as an internal or external command

set aaa="this is a string"
for /f %%i in ('"My Program Name.exe" %%aaa%%') do set OutputString=%%i --> program executes but only |this| is passed to the program, meaning surrounding quotes are missing

set aaa=this is a string
for /f %%i in ('"My Program Name.exe" "%%aaa%%"') do set OutputString=%%i --> "My" is not recognized as an internal or external command

a.s.o.

What am I overlooking ?

Re: For /f Quotes - Run a program with a long file name.

Posted: 2021-Aug-23, 9:45 pm
by Simon Sheppard
Try like this

Code: Select all

for /f %%i in ('""My Program Name.exe" "this is a string""') do set OutputString=%%i
I think this is needed because the FOR command starts a new sub-shell which removes the outer set of double quotes.

Re: For /f Quotes - Run a program with a long file name.

Posted: 2021-Aug-24, 5:35 am
by darwin4ever
No good, the parameter-surrounding quotes aren't passed to the program : --> program executes but only |this| is passed to the program, meaning surrounding quotes are missing

Re: For /f Quotes - Run a program with a long file name.

Posted: 2021-Aug-24, 10:16 am
by Simon Sheppard
The default delimiters of the FOR command are a Space, TAB, comma, Equals or Semicolon.

So you will need to add "delims=" to remove those defaults and then %%i will contain everything.

Re: For /f Quotes - Run a program with a long file name.

Posted: 2021-Aug-24, 12:24 pm
by darwin4ever
Simon Sheppard wrote: 2021-Aug-24, 10:16 am So you will need to add "delims=" to remove those defaults and then %%i will contain everything.
Thanks !!!

The combination of your earlier double-quote-suggestion and the "delims=" solved everything.
Great

Re: For /f Quotes - Run a program with a long file name.

Posted: 2021-Aug-26, 9:58 am
by Simon Sheppard
Glad to be of help :)

[Edited the thread title to be more searchable]

Also a handy utility for testing this kind of thing is EchoArgs.exe it's part of the PowerShell Community Extensions but also works under the CMD shell.