0

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.

4
  • 1
    You can totally avoid the delayed expansion headaches by not doing multi-line command blocks. Just call a subroutine with the relevant parameters and process those. And stop using :: 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. Commented May 10, 2018 at 22:20
  • 3
    var is 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. Commented May 10, 2018 at 22:25
  • 1
    Where in the help file for the IF command does it say to use brackets for the comparison? It also does not show an example of using IF....ELSE like you are trying to use it. Commented May 11, 2018 at 2:15
  • -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 140 should resize the width to 140 whilst maintaining the aspect ratio with the height, what does the additional > and ^ bring to the party? Commented May 11, 2018 at 3:40

1 Answer 1

1
  • Don't use (pseudo-) labels :: inside a for loop (code block). if neccessary use rem
  • the for loop variable ~z modifier returns the file size in bytes

:: Q:\Test\2018\05\10\SO_50281887.cmd
@echo off
SETLOCAL EnableDelayedExpansion
SET maxbytesize=25000
SET logfile=A:\JobLogs\PhotoCompression.log
ECHO Starting compression job at %date% %time% >> %logfile%

FOR /R "C:\My\Directory\" /D %%G IN (*
) DO Call :CheckFiles "%%~fG" || (
    PushD "%%~fG"
    magick mogrify -resize "140>^" -quality 75% -depth 8 -units PixelsPerInch -density 72x72 -strip "*.jpg"
    ECHO %%G has been compressed %date% %time% >> %logfile%
    PopD
)
:: Appends completed job status to logfile
ECHO Job is complete %date% %time% >> %logfile%
Exit

:CheckFiles
For %%F in (%~1\*.jpg) Do If %%~zF GTR %maxbytesize% Exit /B 1
:: No files are larger than 25Kb,
ECHO All files in %~1 are smaller than %maxbytesize% %date% %time% >> %logfile%
  • This batch iterates all (sub-)folders from start and
  • calls a subroutine which checks all *.jpg in there
  • if only one is greater than maxbytesize it returns an error which
  • invokes the magick command through conditional execution.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.