Windows batch file syntax sometimes is a pain compared to UNIX bash/sh. Using optipng I wanted to shrink all the PNG images in a directory and inside its subdirectories as well. Notice that I had to write the command into a .bat batch file, it did not work directly from the command line:
FOR /F "tokens=*" %%G IN ('dir /s /b *.png') DO optipng -nc -nb -o7 -full %%G
The optipng params -nc and -nb prevent any color and color depth changes of the png files. Those may change the appearance, for example in the evil Internet Explorer. -o7 means the best and slowest optimization and -full does a full scan of the IDAT part. Using UNIX the same job may look like:
find . -name *.png | xargs optipng -nc -nb -o7 -full
Thanks for this article! I needed to optimize .png tiles to custom google map type. 🙂
LikeLike
it should be :
find . -name “*.png” | xargs optipng -nc -nb -o7 -full
LikeLike
If you have a directory containing a space the following command should be used:
FOR /F “tokens=*” %%G IN (‘dir /s /b *.png’) DO optipng -nc -nb -o7 -full “%%G”
LikeLike
Thanks! And to feed it the dir from the command line:
FOR /F “tokens=*” %%G IN (‘dir /s /b %1*.png’) DO optipng -nc -nb -o7 -full “%%G”
LikeLike
Thank you good sir, it looks like gibberish but it works.
LikeLike
An alternative using find’s exec switch:
find . -name *.png -exec optipng -nc -nb -o7 -full {} +
The ‘+’ passes all the files to optipng at once, which may be quicker.
LikeLike
find . -type f -name “*.png” -exec optipng -nc -nb -o7 -full {} ;
LikeLike