Page 1 of 1

How to get output of program to variable?

Posted: 30 May 2016 16:16
by doscode
Hello,
I am using Windows XP and batch file where I have this command:

Code: Select all

convert map2.png -format "%%wx%%h" info:

after executing it I see this: 256x158 and I need to save it to variable and then to
paste content of the variable to next command:

Code: Select all

convert
result.png -resize ...here to put the content of the variable...

I have no idea how can I get the result to variable. Is it possible to do it in single batch file?

Re: How to get output of program to variable?

Posted: 30 May 2016 18:39
by elias
Hi,

It is not clear exactly what you are asking but from what I understood, you want to:
1. run a command
2. capture and parse its output
3. the parsed output should be passed to another command?


For (1) and (2), you may want to use the FOR /F syntax with the backquote to capture the output and parse it. Perhaps combine that with FINDSTR, etc.

After you capture the output into variables you can pass them to (3).

Study this example. I think it does what you are asking for:

https://github.com/PassingTheKnowledge/ ... Reveal.bat

Re: How to get output of program to variable?

Posted: 31 May 2016 03:49
by elzooilogico
doscode wrote:Hello,
I am using Windows XP and batch file where I have this command:

Code: Select all

convert map2.png -format "%%wx%%h" info:

after executing it I see this: 256x158 and I need to save it to variable and then to
paste content of the variable to next command:

Code: Select all

convert
result.png -resize ...here to put the content of the variable...
I have no idea how can I get the result to variable. Is it possible to do it in single batch file?
Your idea is not clear for me.

Are %%w & %%h for parameters or are common variables?

The sintax %%var is used only with for parameters in batch context (they are %var in commandline context).

If they are common variables they should be %w%x%h%

But, if I understand and the first command works for you, this may help

Code: Select all

rem if w & h are for parameters use
for /f "usebackq tokens=*" %%i in (`convert map2.png -format "%%wx%%h" info:`) do (
  for /f "usebackq tokens=*" %%n in (`convert result.png -resize %%i`) do (
     echo Output is %%n
  )
)

rem if w & h are standard variables use
for /f "usebackq tokens=*" %%i in (`convert map2.png -format "%w%x%h%" info:`) do (
  for /f "usebackq tokens=*" %%n in (`convert result.png -resize %%i`) do (
     echo Output is %%n
  )
)

%%i holds output of the first for command, and can be used by the second.

Also, you may split 256x158,

Code: Select all

for /f "usebackq tokens=1,2 delims=x" %%i in (`convert map2.png -format "%%wx%%h" info:`) do (
  rem if output is 256x158, now %%i=256 and %%j=158
  for /f "usebackq tokens=*" %%n in (`convert result.png -resize %%ix%%j`) do (
     echo output is %%n
  )
)

Re: How to get output of program to variable?

Posted: 31 May 2016 04:07
by doscode
Thanks for you help. This is what I needed:

Code: Select all

for /f "usebackq tokens=1" %%a in (`convert map2.png -format "%%wx%%h" info:`) do ( echo %%a )


after I run the batch then this is the output:

Code: Select all

>for /F "usebac
kq tokens=1" %a in (`convert map2.png -format "%wx%h" info:`) do (echo %a  )

I:\PC Improve\ImageMagick a GhostScript\Locate sub-image in image>(echo 256x158
 )
256x158


Correct!

Yet I am trying to save the variable from loop to different variable and display the content of the variable outside of the loop. This fails:

Code: Select all

for /f "usebackq tokens=1" %%o in (`convert map2.png -format "%%wx%%h" info:`) do (
  original_size_1 = %%o )
echo %%original_size_1


output:

Code: Select all

>echo %original
_size_1
%original_size_1

Re: How to get output of program to variable?

Posted: 31 May 2016 04:42
by elzooilogico
doscode wrote:Yet I am trying to save the variable from loop to different variable and display the content of the variable outside of the loop. This fails:

Code: Select all

for /f "usebackq tokens=1" %%o in (`convert map2.png -format "%%wx%%h" info:`) do (
  original_size_1 = %%o )
echo %%original_size_1

Please, read carefully the previous post.

%%var syntax is only used with for parameters. Common variables are referenced using %var% syntax. Also take care of the spaces when assigning and referencing variables. Also again, you need to use set to declare a variable.

Code: Select all

for /f "usebackq tokens=1" %%o in (`convert map2.png -format "%%wx%%h" info:`) do (
  set original_size_1=%%o
)
echo %original_size_1%

More, you may enclose the declaration and its content in quotes to avoid odd assigments. But consider the following

Code: Select all

Set "myvar1=This is a string"
Set myvar2="This is a string"
echo %myvar1%
echo %myvar2%

Output is

Code: Select all

This is a string
"This is a string"

As you can see, myvar2 holds exactly "This is a string" where myvar1 holds This is a string, the quotes surrounding both "var-name=var-content" avoid (for instance) trailing spaces.

Re: How to get output of program to variable?

Posted: 31 May 2016 04:47
by doscode
Outside loop I tried to reference by %var% but because of the other errors it did not work. Now works. Thanks

Code: Select all

for /f "usebackq tokens=1" %%o in (`convert map2.png -format "%%wx%%h" info:`) do (
  set original_size_1=%%o )
echo %original_size_1%