Assign cmdline output to var with inner cmdline quotes?
Moderator: DosItHelp
Assign cmdline output to var with inner cmdline quotes?
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
"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?
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:
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.
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"
%$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?
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:
The double double quotes are essential for it
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
Re: Assign cmdline output to var with inner cmdline quotes?
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?
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?
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?
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"
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?
The first one doesn't work because you are using USEBACKQ which requires the use of back ticks.pstein wrote: ↑15 Jul 2020 04:05Again: 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 second one doesn't work because you misspelled TOKENS as tokes.
Re: Assign cmdline output to var with inner cmdline quotes?
T3RRY wrote: ↑12 Jul 2020 03:00The 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:
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.Code: Select all
For /F "Tokens=* UsebackQ" %%G in (`Command`) Do Set "Var=%%G"
%$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?
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?
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?
Did you notice the difference between a `-character (== backquote / grave accent; used in T3RRY's suggestion) and a '-character (== single quote)?
penpen
penpen
Re: Assign cmdline output to var with inner cmdline quotes?
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
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