@echo off
:LOOP
if %1== GOTO END
SET NEWSIZE=0
SET SIZEDIFF=0
jpegtran -optimize "%~1" "%~1.new"
if errorlevel 1 del "%~1.new" & echo %~nx1 ... jpegtran internal error. Skipped.
for %%x in ("%~1.new") do SET NEWSIZE=%%~zx
SET /A SIZEDIFF=%~z1-%NEWSIZE%
if %NEWSIZE% EQU 0 del "%~1.new" & echo %~nx1 ... Zero byte output. Skipped.
if %SIZEDIFF% LSS 0 del "%~1.new" & echo %~nx1 ... Output [%NEWSIZE%] is larger than original [%~z1]. Skipped.
if exist "%~1.new" (
echo %~nx1 ... from %~z1 to %NEWSIZE% [%SIZEDIFF%].
move /y "%~1.new" "%~1"
)
shift
GOTO LOOP
:END
echo Done.
SET NEWSIZE=
SET SIZEDIFF=
pause