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 ?
For /f Quotes - Run a program with a long file name.
-
- Posts: 6
- Joined: 2021-Aug-16, 12:41 pm
For /f Quotes - Run a program with a long file name.
Last edited by darwin4ever on 2021-Aug-24, 5:36 am, edited 2 times in total.
- Simon Sheppard
- Posts: 127
- Joined: 2021-Jul-10, 7:46 pm
- Contact:
Re: For /f Quotes - Run a program with a long file name.
Try like this
I think this is needed because the FOR command starts a new sub-shell which removes the outer set of double quotes.
Code: Select all
for /f %%i in ('""My Program Name.exe" "this is a string""') do set OutputString=%%i
-
- Posts: 6
- Joined: 2021-Aug-16, 12:41 pm
Re: For /f Quotes - Run a program with a long file name.
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.
- Simon Sheppard
- Posts: 127
- Joined: 2021-Jul-10, 7:46 pm
- Contact:
Re: For /f Quotes - Run a program with a long file name.
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.
So you will need to add "delims=" to remove those defaults and then %%i will contain everything.
-
- Posts: 6
- Joined: 2021-Aug-16, 12:41 pm
Re: For /f Quotes - Run a program with a long file name.
Thanks !!!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.
The combination of your earlier double-quote-suggestion and the "delims=" solved everything.
Great
- Simon Sheppard
- Posts: 127
- Joined: 2021-Jul-10, 7:46 pm
- Contact:
Re: For /f Quotes - Run a program with a long file name.
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.

[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.