1.

Solve : Searching for specific string in previous command output and using in next cmd?

Answer»

Hi All

I'm trying to write a batch file performing steps I've never done before (I've only done some basic stuff before).
I did search for answers but not sure exactly what to search for (terminology to use etc).

This is what I'm currently doing manually (image conversion) that I want to automate with a batch file:

I first need to identify which frame in a *.ICO file contain the 128x128 image. There is two ways to do this.

Option1:
D:\>identify icon1.ico
icon1.ico[0] ICO 16x16 16x16+0+0 32-bit DirectClass 82.7KB 0.000u 0:00.001
icon1.ico[1] ICO 32x32 32x32+0+0 32-bit DirectClass 82.7KB 0.000u 0:00.020
icon1.ico[2] ICO 48x48 48x48+0+0 32-bit DirectClass 82.7KB 0.000u 0:00.027
icon1.ico[3] ICO 128x128 128x128+0+0 32-bit DirectClass 82.7KB 0.000u 0:00.037

Option2:
D:\>convert icon1.ico -format "%s %wx%h" info:
0 16x16
1 32x32
2 48x48
3 128x128

After determining with this image that frame 3 is 128x128 I then convert the image:
D:\>convert icon1.ico[3] icon1.xpm

From the searches it seems an option is to output the info from Option1 or Option2 to a text file. Then use findstr to search through the text. But this is where I fall off the bus!!

I can search for 128x128, but how do I return the correct frame to the convert command?
I assume it will be easier to use option1 which specified the full 'frame name' to use i.e. icon1.ico[3]?
Or is there a different option instead of findstr?

This process repeats then for icon2 to 4.

Thanks in advance!I tried identify, but it didn't work, so i'm just guessing right now, but try
Code: [Select]identify icon1.ico | find "128x128"
if %errorlevel% EQU 0 convert icon1.ico[3] icon1.xpm
REM this checks if there is text output from 'identify icon1.ico' that is = "128x128"
REM this will not work if the frams 128x128 is not [3].
dont know if this helps you much, but its a start


EDIT
this will find the number that 128x128 is
Code: [Select]set a=0
:loop
convert icon1.ico -format "%s %wx%h" info: | find "%a% 128x128"
if %errorlevel% EQU 0 goto break
set /a a+=1
REM this checks if there is text output from 'identify icon1.ico' that is = "128x128"
REM this will not work if the frams 128x128 is not [3].
goto loop
:break
convert icon1.ico[%a%] icon1.xpm
ThanksI forgot to mentioned, I'm using ImageMagick to do the conversion from command line.

Thanks for the suggestion. Got it to work nicely.

When running the bat the "%s %wx%h" part was not read correctly. Ended up with convert icon.ico -format "wx instead and the batch would fail.

I had to use double % ("%%s %%wx%%h") to get it working.

I can now happily loop through the various frames and convert the correct frame/resolution.Quote from: Chaka on JULY 12, 2012, 02:40:15 AM

I forgot to mentioned, I'm using ImageMagick to do the conversion from command line.

Thanks for the suggestion. Got it to work nicely.

When running the bat the "%s %wx%h" part was not read correctly. Ended up with convert icon.ico -format "wx instead and the batch would fail.

I had to use double % ("%%s %%wx%%h") to get it working.

I can now happily loop through the various frames and convert the correct frame/resolution.

Quote
C:\>for /?
Runs a specified command for each file in a set of files.

FOR %variable IN (set) DO command [command-parameters]

%variable Specifies a single letter replaceable parameter.
(set) Specifies a set of one or more files. Wildcards may be used.
command Specifies the command to carry out for each file.
command-parameters
Specifies parameters or switches for the specified command.

To use the FOR command in a batch PROGRAM, specify %%variable instead
of %variable. Variable names are CASE sensitive, so %i is different
from %I.
Quote from: Chaka on July 12, 2012, 02:40:15 AM
I forgot to mentioned, I'm using ImageMagick to do the conversion from command line.

Thanks for the suggestion. Got it to work nicely.

When running the bat the "%s %wx%h" part was not read correctly. Ended up with convert icon.ico -format "wx instead and the batch would fail.

I had to use double % ("%%s %%wx%%h") to get it working.

I can now happily loop through the various frames and convert the correct frame/resolution.

Sorry to put you through that: just realized
Code: [Select]convert icon1.ico -format "%s %wx%h" info: | find "%a% 128x128"
was interchangable with
Code: [Select]identify icon1.ico | find "icon1.ico[%a%] ICO 128x128"


Discussion

No Comment Found