Search code examples
windowsloopsfor-loopbatch-filecmd

For loop in batch file won't stop


My for loop just keeps going and going. I'm guessing around 6 times till it quits. With 14,000 files, that takes some time. All I want it to do is do it once. Just once, not to do it again and again. I've tested with a directory of 6 files and it eventually quits, but by that time it has repeated itself numerous times.

Here is the code for use in the command prompt (I have removed the %% which was in the batch file).

start "Adjustments 1" cmd /C "FOR %a in (*.png) DO magick %a -adaptive-sharpen 0x5 modified\adaptive_sharpen2_%a & FOR %a in (*.png) DO magick %a -adaptive-blur 0x1 modified\adaptive_blur_%a"

Please, how do I get this to only process the files once instead of processing each png multiple times and continually overwriting?


Solution

  • For performing the task how you'd intended, you should isolate one for loop from the other. This can be done using parentheses:

    Start "Adjustments 1" cmd.exe /S /Q /D /C "(For %G In (*.png) Do magick.exe "%G" -adaptive-sharpen 0x5 "modified\adaptive_sharpen2_%~G") & For %G In (*.png) Do magick.exe "%G" -adaptive-blur 0x1 "modified\adaptive_blur_%~G""
    

    However, I don't understand why you're joining a for loop to the end of another, when they're enumerating the exact same file listing. It is just inefficient to do it like that.

    Would this not do the same thing:

    Start "Adjustments 1" cmd.exe /S /Q /D /C "For %G In (*.png) Do (magick.exe "%G" -adaptive-sharpen 0x5 "modified\adaptive_sharpen2_%~G" & magick.exe "%G" -adaptive-blur 0x1 "modified\adaptive_blur_%~G")"