Assign cmdline output to var with inner cmdline quotes?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Assign cmdline output to var with inner cmdline quotes?

#1 Post by pstein » 12 Jul 2020 02:00

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

T3RRY
Posts: 249
Joined: 06 May 2020 10:14

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

#2 Post by T3RRY » 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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

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

#3 Post by jeb » 13 Jul 2020 01:32

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

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

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

#4 Post by pstein » 14 Jul 2020 00:27

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?

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

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

#5 Post by Squashman » 14 Jul 2020 09:55

The syntax from the help file for the FOR command clearly states

Code: Select all

tokens=

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

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

#6 Post by pstein » 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"

Squashman
Expert
Posts: 4484
Joined: 23 Dec 2011 13:59

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

#7 Post by Squashman » 15 Jul 2020 08:04

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.

T3RRY
Posts: 249
Joined: 06 May 2020 10:14

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

#8 Post by T3RRY » 15 Jul 2020 08:09

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

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

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

#9 Post by pstein » 16 Jul 2020 05:27

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?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

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

#10 Post by penpen » 16 Jul 2020 06:24

Did you notice the difference between a `-character (== backquote / grave accent; used in T3RRY's suggestion) and a '-character (== single quote)?


penpen

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

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

#11 Post by jeb » 16 Jul 2020 12:14

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

Post Reply