I am writing a batch file that will search a directory for .jpg files larger than 25Kb and then run a command to compress those files.
Currently, the command will compress every .jpg file when I run it against the entire directory, but I am struggling to get the command to run only for files greater than 25Kb in said directory. Code is below:
@echo off
SETLOCAL EnableDelayedExpansion
SET maxbytesize=25000
SET logfile=A:\JobLogs\PhotoCompression.log
ECHO Starting compression job at %date% %time% >> %logfile%
:: Runs the following if/else statement against all files in the directory
FOR %%G IN (C:\My\Directory) DO (
:: If file size is greater than or equal to 25Kb, run the ImageMagick command and append the name of the processed file to logfile
IF [ %var% GTR %maxbytesize% ]
magick mogrify -resize "140>^" -quality 75% -depth 8 -units PixelsPerInch -density 72x72 -strip *.jpg
ECHO %%G has been compressed %date% %time% >> %logfile%
:: No files are larger than 25Kb, write "All files are compressed" to logfile
ELSE ECHO All files are compressed %date% %time% >> %logfile%
)
:: Appends completed job status to logfile
ECHO Job is complete %date% %time% >> %logfile%
Exit
The command "magick mogrify -resize "140>^" -quality 75% -depth 8 -units PixelsPerInch -density 72x72 -strip *.jpg" is an ImageMagick command and can be ignored as it does successfully compress photos when ran against the entire directory as mentioned above.
Note that I have tried variations of %G, %%G, %G%, and %var% in the if condition with no success.
::in place of REM, it's an undocumented hack that takes advantage of a bug in cmd.exe that could actually get fixed one day.varis not defined in your script. Either that's a bug or you've not posted an MCVE. Take the tour, read How to Ask, and minimal reproducible example.-resize "140>^"looks wrong!width: Width given, height automagically selected to preserve aspect ratio.widthxheight^: Minimum values of width and height given, aspect ratio preserved.widthxheight>Shrinks an image with dimension(s) larger than the corresponding width and/or height argument(s). It appears that-resize 140should resize the width to140whilst maintaining the aspect ratio with the height, what does the additional>and^bring to the party?