Categories
Windows

A batch file of jpegtran for overwriting on Windows.

同一記事の日本語版
Update information      Edit(2016.Oct.29)

   When I use jpegtran on Windows, it’s very inconvenient. So I made a batch file for myself, which overwrites the original jpeg file with the new optimize jpeg file.
@echo off
setlocal enabledelayedexpansion
pushd %~dp0
for %%a in (%*) do (
set OutFile=%%~na%%~xa
jpegtran -copy none -optimize -outfile !OutFile! %%a
for %%b in (!OutFile!) do set fileSize=%%~zb
if !fileSize! LSS %%~za (copy !OutFile! %%a>nul)
del !OutFile!
)
popd
exit
   Bacicaly, the batch file replaces original jpeg files with new ones, but it leaves original files instead new ones when the new file is bigger than the original. This sometimes occurs if the original was created by some graphics editors. Around 100 files possible at one time. This maybe depends on cmd.exe ability itself, I think.

   How to use the batch file:

  1. Copy & paste the above codes to a text editor, and save it as a batch file named ‘jpegtran.bat’ or something. You can have ‘jpegtran.txt’ from here. Change its extension from txt to bat.
  2. Copy the batch file and jpegtran.exe to the same folder.
  3. Drag & Drop the jpeg files you want to optimize onto the batch icon.
  4. That’s it.

   If you use the batch file, please remember the followings. This batch file makes overwriting, so it leaves no original files.

   I optimized all jpeg files in my sites. Now, I have no suggestion about jpeg files from PageSpeed Insights. Clap, clap.

References:
   1. List of DOS commands (Japanese)
   2. Jpegtran’s help

Edit(2016.Oct.29):
   This post has a few visitors. So, I added some though I wrote this long before 😋.

   You can place the folder (in which, jpegtran.exe and jpegtran.bat) anywhere in your PC. For your convenience, you should create a shortcut of jpegtran.bat on your Desktop. The shortcut ability equals jpegtran.bat itself.
   You can do bulk actions for jpeg files are in some ranges by the batch file. Search jpeg files for the ranges and “Select All” then Drag&Drop.

Leave a Reply

Your email address will not be published. Required fields are marked *