mogrify - how do you recurse through subfolders in Windows

2

1

I thought there was a simple switch in mogrify command options that tells it go through subfolders as well. I didn't find any. On Linux, the "find" command seems to do the trick, how do you do this on Windows?

magick mogrify -resample 72 -resize 700x700 -format png -path "C:\ImageMagick-7.0.8-Q16\dest" *.*

works but I need it to recurse through subfolders as well. What's the simplest way to do that?

Ideally, I'm looking for the source directory structure to be retained in destination but with the processed images only.

Regmi

Posted 2018-07-13T16:39:44.710

Reputation: 725

Answers

2

ImageMagick Mogrify Files Recursively - Windows

You can use a FOR /R loop to iterate the files starting from the root directory the files reside and then run those over the commands accordingly as per the below example against each file.

If you omit the -path parameter it seems that it will run against the original files where they reside. So for a simple solution to retain the original structure from the original source, consider simply copying the original root folder of the source files to a new location and then rename that folder.


Example Command

Note: Be sure to replace C:\Source\Folder\Root to be the exact folder where the files or file subfolders reside that it will recurse to run the command operations against those files.

for /r "C:\Source\Folder\Root" %a in (*.*) do mogrify -resample 72 -resize 700x700 -format png "%~a"

Example Command (with delete)

for /r "C:\Source\Folder\Root" %a in (*.*) do mogrify -resample 72 -resize 700x700 -format png "%~a" && IF NOT [%~Xa]==[.png] DEL /Q /F "%~a"

Further Resources

  • FOR /R
  • Batch Substitutions (FOR /?)

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    %~xI        - expands %I to a file extension only
    
  • mogrify

  • Del

Pimp Juice IT

Posted 2018-07-13T16:39:44.710

Reputation: 29 425

Works like a charm. Thanks! I do need to be able to delete the original files as they're very large tiffs and I do not want them in the output. I can make copies of the original folder structure so no need for the script to create a new folder structure for the output, it could be an inplace overwrite. – Regmi – 2018-07-13T21:13:41.913

Will do! One more thing. If I add a switch like -profile "sRGB.icc", do I need to escape it in the command because of the quotes I'm using? example: for /r "C:\ImageMagick-7.0.8-Q16\source" %a in (.) do magick mogrify -profile "sRGB.icc" -resample 72 -resize 700x700 -format png "%~a" – Regmi – 2018-07-13T21:48:07.617

I figured it, no worries. the command is perfectly correct, it looks like adding the profile to PNG is an issue. – Regmi – 2018-07-13T21:53:01.907