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

Microsoft Windows
Post Reply
darwin4ever
Posts: 6
Joined: 2021-Aug-16, 12:41 pm

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

Post 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 ?
Last edited by darwin4ever on 2021-Aug-24, 5:36 am, edited 2 times in total.
User avatar
Simon Sheppard
Posts: 190
Joined: 2021-Jul-10, 7:46 pm
Contact:

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

Post 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.
darwin4ever
Posts: 6
Joined: 2021-Aug-16, 12:41 pm

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

Post 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
Last edited by darwin4ever on 2021-Aug-24, 12:22 pm, edited 1 time in total.
User avatar
Simon Sheppard
Posts: 190
Joined: 2021-Jul-10, 7:46 pm
Contact:

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

Post 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.
darwin4ever
Posts: 6
Joined: 2021-Aug-16, 12:41 pm

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

Post 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
User avatar
Simon Sheppard
Posts: 190
Joined: 2021-Jul-10, 7:46 pm
Contact:

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

Post 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.
Post Reply