for-loopbatch-filerecursionsubdirectory

Windows batch file to recursively use Imagemagick command to process .jpg images in a folder and subfolders?


I'm successfully using this batch file in each subdirectory to process hundreds of .jpg images:

@echo off
for %%A in (*.jpg) do (
    "C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\magick" mogrify -gravity South -chop 0x42 -shave 14x0 "%%~nxA"
)

But I have to paste this bat file into each directory and then run it in each individually. So I'm asking if there's a way to make this work recursively, by just running it once inside the base directory.

I have tried various methods using /r, but none of them have been successful. I would appreciate some help if possible!


Solution

  • After trying out several combinations with /r and /f and without /r, the one that worked for me perfectly is:

    @echo off
    for /r %%f in (*.jpg) do (
        "C:\Program Files\ImageMagick-7.1.1-Q16-HDRI\magick" mogrify -gravity South -chop 0x42 -shave 14x0 "%%f"
    )
    

    This worked beautifully across the base directory and all sub-directories and nested one's too :) Thanks very much for the tips!