Need help with looping over directories

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
seanspotatobusiness
Posts: 8
Joined: 2013-11-19T05:23:43-07:00
Authentication code: 6789

Need help with looping over directories

Post by seanspotatobusiness »

Could someone provide the code for looping through directories? I'd like to convert my (40 GB total!) uncompressed TIFFs to LWZ-compressed TIFFs but maintaining directory structure/hierarchy.
Last edited by seanspotatobusiness on 2014-05-13T02:44:44-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Need help watermarking images in Image Magick!

Post by fmw42 »

seanspotatobusiness wrote:Could someone provide the code for looping through directories? I'm in a similar situation with respect to maintaining directory structure/hierarchy except I want to convert my (40 GB total!) uncompressed TIFFs to LWZ-compressed TIFFs.
Code depends upon your OS (Windows or Unix)? Also what version of IM are you using?

In the future, please start a new topic rather than tacking onto an old one. Also please read viewtopic.php?f=1&t=9620
seanspotatobusiness
Posts: 8
Joined: 2013-11-19T05:23:43-07:00
Authentication code: 6789

Re: Need help with looping over directories

Post by seanspotatobusiness »

I'm using Windows 7 and ImageMagick version is 6.8-7.6 Q16 x64 2013-11-07.

I thought my query was still relevant to the older thread because although the solution of looping over directories was mentioned, there was no indication of how that should be done.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Need help with looping over directories

Post by snibgo »

IM contains a "mogrify" command for processing multiple files files in one directory, but it won't walk a directory tree. So you need to use an operating system facility for that.

I would use "for" to get individual files, and "convert" to convert one at a time. Type "for /?" for help. For example, at the command line:

Code: Select all

for /F %F in ('dir /b /s *.tiff') do echo %~dpF %~nF
Instead of echo, you would have a convert command, which would depend on exactly what you want to do. For example "convert %F -compress LZW %F" would compress each file, overwriting it. You might prefer to create copies in a separate directory structure.
snibgo's IM pages: im.snibgo.com
Post Reply