Page 1 of 1

Assign cmdline output to var with inner cmdline quotes?

Posted: 12 Jul 2020 02:00
by pstein
Assume I have a cmdline command simplified as follows:

"D:\aaa\bbb cccc\ddd eee\myprog.exe" -parm1 "https://www.foobar.com/web/page/targetpage/q=$subparm" xxxx.jpg

Since the program path contains blanks it must be enclosed in double quotes.

The command above works in CmdPrompt on 64bit Win7

Now I want to assign the output of the command (=an URL) to a DOS batch variable.

As in the past I tried to code the following:

%$set% output="D:\aaa\bbb cccc\ddd eee\myprog.exe" -parm1 "https://www.foobar.com/web/page/targetpage/q=$subparm" xxxx.jpg
echo out=%output%

However this does not work. The variable %output% contains nothing.

Why?

How else do I have to code the assignment in DOS batch file?
Peter

Re: Assign cmdline output to var with inner cmdline quotes?

Posted: 12 Jul 2020 03:00
by T3RRY
The set command only assigns a string to a variable. In order to capture the output of a command for assignment, A 'For /F' Loop is required

The typical syntax for iterating over a command containing quotes within a For /F loop is:

Code: Select all

For /F "Tokens* UsebackQ" %%G in (`Command`) Do Set "Var=%%G"
I must say, I'm also curious as to what the purpose of the Variable %$Set% is about, given your OP Made No mention of assigning $Set with any value that would expand in a manner that could result in the assignment of any value to a variable.
%$set% output="D:\aaa\bbb cccc\ddd eee\myprog.exe" -parm1 "https://www.foobar.com/web/page/targetpage/q=$subparm" xxxx.jpg

Re: Assign cmdline output to var with inner cmdline quotes?

Posted: 13 Jul 2020 01:32
by jeb
Hi T3rry,

I suppose the $set is a macro, perhaps the one from SO: Assign output of a program to a variable using a MS batch file.

But even then it should work with spaces, I tested it and it works for me with:

Code: Select all

call :initMacro

%$set% output=""with space\calc.exe" -parm1 "https://www.foobar.com/web/page/targetpage/q=$subparm" xxxx.jpg "
set output
The double double quotes are essential for it

Re: Assign cmdline output to var with inner cmdline quotes?

Posted: 14 Jul 2020 00:27
by pstein
You are partially right.

I wrote down the %$set% assignment trick in the past but did not found not the working example in my archive.

Anyway I am still searching for a way to assign the output of a command to a batch variable otherwise.

For /F "Tokens* UsebackQ" %%G in (`"D:\aaa\bbb cccc\ddd eee\myprog.exe" -parm1 "https://www.foobar.com/web/page/targetpage/q=$subparm" xxxx.jpg`) Do Set "Var=%%G"

does not work either.
I am getting a "Tokens* UsebackQ" was unexpected at this time."

What else can I do?

Re: Assign cmdline output to var with inner cmdline quotes?

Posted: 14 Jul 2020 09:55
by Squashman
The syntax from the help file for the FOR command clearly states

Code: Select all

tokens=

Re: Assign cmdline output to var with inner cmdline quotes?

Posted: 15 Jul 2020 04:05
by pstein
Again: This does not help.

When I code in the dos batch script:

For /F "usebackq tokens=*" %%G in ('"D:\aaa\bbb cccc\ddd eee\myprog.exe" -parm1 "https://www.foobar.com/web/page/targetpage/q=$subparm" xxxx.jpg') Do Set "Var=%%G"

I am getting an abort:

"D:\aaa\bbb is not recognized as an internal or external command,operable program or batch file."

Same if I use real backticks like in:

For /F "usebackq tokes=*" %%G in (`"D:\aaa\bbb cccc\ddd eee\myprog.exe" -parm1 "https://www.foobar.com/web/page/targetpage/q=$subparm" xxxx.jpg`) Do Set "Var=%%G"

Re: Assign cmdline output to var with inner cmdline quotes?

Posted: 15 Jul 2020 08:04
by Squashman
pstein wrote:
15 Jul 2020 04:05
Again: This does not help.

When I code in the dos batch script:

For /F "usebackq tokens=*" %%G in ('"D:\aaa\bbb cccc\ddd eee\myprog.exe" -parm1 "https://www.foobar.com/web/page/targetpage/q=$subparm" xxxx.jpg') Do Set "Var=%%G"

I am getting an abort:

"D:\aaa\bbb is not recognized as an internal or external command,operable program or batch file."

Same if I use real backticks like in:

For /F "usebackq tokes=*" %%G in (`"D:\aaa\bbb cccc\ddd eee\myprog.exe" -parm1 "https://www.foobar.com/web/page/targetpage/q=$subparm" xxxx.jpg`) Do Set "Var=%%G"
The first one doesn't work because you are using USEBACKQ which requires the use of back ticks.

The second one doesn't work because you misspelled TOKENS as tokes.

Re: Assign cmdline output to var with inner cmdline quotes?

Posted: 15 Jul 2020 08:09
by T3RRY
T3RRY wrote:
12 Jul 2020 03:00
The set command only assigns a string to a variable. In order to capture the output of a command for assignment, A 'For /F' Loop is required

The typical syntax for iterating over a command containing quotes within a For /F loop is:

Code: Select all

For /F "Tokens=* UsebackQ" %%G in (`Command`) Do Set "Var=%%G"
I must say, I'm also curious as to what the purpose of the Variable %$Set% is about, given your OP Made No mention of assigning $Set with any value that would expand in a manner that could result in the assignment of any value to a variable.
%$set% output="D:\aaa\bbb cccc\ddd eee\myprog.exe" -parm1 "https://www.foobar.com/web/page/targetpage/q=$subparm" xxxx.jpg

Re: Assign cmdline output to var with inner cmdline quotes?

Posted: 16 Jul 2020 05:27
by pstein
No, you didn't understand me.

You simply wrote (`Command`).

But what if the Command in turn contains inside further, nested double quotes?

Double quotes are sometimes required if the path to a program contains blanks (see here the blank between bbb and ccc).

If I write for example:

For /F "Tokens=* UsebackQ" %%G in ('"D:\aaa\bbb cccc\ddd eee\myprog.exe" -parm1 "https://www.foobar.com/web/page/targetpage/q=$subparm" xxxx.jpg) Do Set "Var=%%G"

I get an The system cannot find the file -parm1.

If I enclose the full command on the other hand in single quotes:

For /F "Tokens=* UsebackQ" %%G in ('"D:\aaa\bbb cccc\ddd eee\myprog.exe" -parm1 "https://www.foobar.com/web/page/targetpage/q=$subparm" xxxx.jpg') Do Set "Var=%%G"

....the command is not executed and var contains the whole cmdstring.

I want to get the output of the command in a string.

Can I/Do I have to mask the inner double quotes somehow?

Any other suggestions?

Re: Assign cmdline output to var with inner cmdline quotes?

Posted: 16 Jul 2020 06:24
by penpen
Did you notice the difference between a `-character (== backquote / grave accent; used in T3RRY's suggestion) and a '-character (== single quote)?


penpen

Re: Assign cmdline output to var with inner cmdline quotes?

Posted: 16 Jul 2020 12:14
by jeb
Hi pstein,

did you test your code with the macro from SO:Assign output of a program to a variable?

It should work without any modifications.

The FOR/F loop uses a single quote and a double quote

Code: Select all

for /F "delims=" %%O in ('"%%~2 | findstr /N ^^"') do ( %\n%
--- For beter visibility: ... %%O in ( ' " %%~2