Batch Mogrify with Sub Folders HELP!

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Jenkins

Batch Mogrify with Sub Folders HELP!

Post by Jenkins »

Hello all,

I have come across this problem before but now it is more prevalent than ever;

Here is a quick example of what i'm trying to achieve

Code: Select all

SET IMDIR=C:\Program Files\ImageMagick-6.5.1-Q16

SET APP=mogrify.exe
SET OPTS=-format tga *.dds
REM mogrify Usage:
REM mogrify -format tga *.dds

CD D:\DDS_TEMP
"%IMDIR%\%APP%" %OPTS%

PAUSE
Which returns the error
mogrify.exe: unable to open image `*.dds': No such file or directory @ blob.c/OpenBlob/2439.
mogrify.exe: unable to open image `*.dds': Invalid argument @ blob.c/OpenBlob/2439.
I know what this means because i've had to workaround it before... But now instead of a workaround (would takes hours to code) i would like to know how to process multiple sub folders with ONE line of command instead of 'CD'ing or 'PUSHD'ing to each folder that has images directly in it.

For example, the directory DDS_TEMP has more than 5000 dds materials in it with folders in folders in folders, all i want to do is use the "-format tga *.dds" option to convert them all to TGA (with output images in the source directory they came from)

I've searched the forum, looked through the Usage examples and documentation with no avail.

Here is my workaround that i DO NOT want to use...

Code: Select all

PUSHD D:\DDS_TEMP\Game\UI\HUD1
"%IMDIR%\%APP%" %OPTS%
POPD
PUSHD D:\DDS_TEMP\Game\UI\HUD2
"%IMDIR%\%APP%" %OPTS%
POPD
PUSHD D:\DDS_TEMP\Game\UI\HUD3
"%IMDIR%\%APP%" %OPTS%
POPD
PUSHD D:\DDS_TEMP\Game\UI\HUD4
"%IMDIR%\%APP%" %OPTS%
POPD
etc (there are over 1000 different folders that i dont want to write a PUSHD command for each)

Also IM is the only program that can properly convert a DDS texture into a 32bpp TGA and not destroy the Alpha channel, programs like ImageConverterPlus destroys the Alpha channel which renders most of the textures useless (Also the aforementioned program does not write to 32bpp TGA files)

Any help is greatly appreciated and will save alot of time on pretty pointless coding imo.

Thanks in advance, Jenkins
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Batch Mogrify with Sub Folders HELP!

Post by el_supremo »

The FOR loop will do what you want. You can use it to enumerate the subdirectories (including the current one) and then execute mogrify on each one.
I can't completely test this but it should work:

Code: Select all

SET IMDIR=C:\Program Files\ImageMagick-6.5.1-Q16

SET APP=mogrify.exe
SET OPTS=-format tga %%~fD\*.dds
REM mogrify Usage:
REM mogrify -format tga *.dds

CD D:\DDS_TEMP
for /r . %%D in (.) do "%IMDIR%\%APP%" %OPTS%

PAUSE
The "%%~fD" will extract the pathname from the variable D which for each iteration of the FOR loop will be one of the subdirectories (including the current one)

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Jenkins

Re: Batch Mogrify with Sub Folders HELP!

Post by Jenkins »

IT WORKS! :D

Only one small issue that may be an Imagemagick problem i'm not to sure, if the path has spaces in the name even with quotes it breaks the script...
Pathname: D:\Path With Spaces\
Command: CD "D:\Path With Spaces\"

mogrify.exe: unable to open image `D:\Path': No such file or directory @ blob.c/OpenBlob/2439.
mogrify.exe: unable to open image `With\*.dds': No such file or directory @ blob.c/OpenBlob/2439.
mogrify.exe: unable to open image `Spaces\*.dds': No such file or directory @ blob.c/OpenBlob/2439.
Or something to that effect anyway, i used a path with no spaces and simply PUSHD "%1" which rectified the problem. No biggy :)

Not really an issue because the workaround is alot simpler than my previous problem/workaround.

Thank you Thank you Thank you Thank you Thank you x10000 :D :D

FOR loops have never been my forte because i use batch whenever software can't do the job properly, which in this case no other Image converting programs could keep the alpha plus convert them to 32bpp TGA...

Once again thank you and have a wonderful day! 8)

-Jenkins
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Batch Mogrify with Sub Folders HELP!

Post by el_supremo »

Jenkins wrote:if the path has spaces in the name even with quotes it breaks the script...
I can get it to work with spaces in the filename and/or directory path if the path/filename is quoted but it requires a slight mod to the original script. Try this:

Code: Select all

SET IMDIR=C:\Program Files\ImageMagick-6.5.1-Q16

SET APP=mogrify.exe
SET OPTS=-format tga
REM mogrify Usage:
REM mogrify -format tga *.dds

CD D:\DDS_TEMP
for /r . %%D in (.) do "%IMDIR%\%APP%" %OPTS% "%%~fD\*.dds"

PAUSE
Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Jenkins

Re: Batch Mogrify with Sub Folders HELP!

Post by Jenkins »

Thanks Pete!

Worked like a charm mate thank you very much :D

-Jenkins
Post Reply