Batch PNGOut File Compressor

This is a batch file I wrote to handle the compression directories full of PNG files using Ken Silverman’s amazing PNGOUT tool.

Bear in mind that this isn’t exactly a replacement for PNGOUTWin which supports asynchronous file compression taking advantage of multiple cores, this is merely a batch file that recursively navigates through a given directory and throws all PNGs one at a time at PNGOUT.

Output example:

Output for D:\Images\ for 88 files
1252 kb before
922 kb after
saving a total of 330 kb (26%)
Press any key to continue . . .

Instructions:

  1. Download Ken Silverman’s command line PNGOUT tool (as provided in the link)
  2. Set the path for the pngo variable in the batch file
  3. Run the batch file simply like so
    • pngout-batch “D:\Images\”
  4. Optional: Create a shortcut to the batch file and place it in your SendTo folder for your user account for the sake of convenience
    • Windows XP
      • C:\Documents and Settings\<username>\SendTo
    • Windows Vista / 7
      • C:\Users\<username>\AppData\Roaming\Microsoft\Windows\SendTo

Here is a preview of the batch file or you can download the batch file itself at the bottom of the post.

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: PNGOut Compressor Batch File v1.0
:: 
:: Author: Terry J. Butler
:: Website: http://www.terrybutler.co.uk/
:: Post URI: http://www.terrybutler.co.uk/2011/07/07/batch-pngout-file-compressor/
:::READ ME::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Distribute this batch file as you'd like but I ask that you please leave this 
:: commented header section intact.
:: If you want to modify this batch file, I ask that you give credit where credit 
:: is due.
:: 
:: Thanks to Ken Silverman for the fantastic PNGOut. Nothing compares to it.
:: You'll need to download it from http://advsys.net/ken/utils.htm, place it  
:: somewhere convenient and change the path to it for the 'pngo' variable under the
:: "CONFIGURATION" header
::
:: Thanks to the NT reference at SS64.com (http://ss64.com/nt/) for an incredible 
:: wealth of information that helped shape this batch file
:: 
:: Head to the link in the Post URI above for further details on how to use.
:::CHANGE LOG:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
::
:: v1.0
:: - Initial Release
::
:::CONFIGURATION::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Set this path to the locaation of your pngout.exe file
SET pngo="D:\tools\pngout.exe" 
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

@echo off

:: Setting EnabledDelayedExpansion will cause each variable to be expanded at execution 
:: time rather than at parse time. EnableDelayedExpansion is Disabled by default.
setLocal EnableDelayedExpansion

:: set some arithmetic capable variables
set /a value=0
set /a sum=0
set /a sumOriginal=0
set /a count=0
set /a diff=0
set /a diffPercent=0

pushd %1

	FOR /R %%G IN (*.png) DO (
		echo Now Compressing %%G 
		
		set /a value=%%~zG
		set /a sum=!sum!+!value!	
		set /a count+=1
		%pngo% "%%G"
	)

	set /a sumOriginal=sum/1024

	set /a value=0
	set /a sum=0

	FOR /R %%G IN (*.png) DO (
		set /a value=%%~zG
		set /a sum=!sum!+!value!	
	)
	
	set /a sum=sum/1024	
	set /a diff=!sumOriginal!-!sum!	
	set /a diffPercent=(100*(!sumOriginal!-!sum!))/!sumOriginal!;	

	@echo Output for %CD% for %count% files
	@echo !sumOriginal! kb before
	@echo !sum! kb after	

	@echo saving a total of !diff! kb (!diffPercent!%% decrease)
	
popd

:: Unset variables
SET _value=
SET _sum=
SET _sumOriginal=
SET _count=
SET _diff=
SET _diffPercent=

pause

Download pngout-batch.bat

10 comments

  1. Thank you, this has been very easy to understand and follow, the bat file works perfectly giving info that pngout would not normally give. One side note is that upon using send to it does batch the whole of that folder not just a single file (this may seem obvious to most you), if only require a single image then suggest put it in a new folder temporarily just to process.
    Many Thanks
    Magicake

  2. Hi Magicake

    I’m glad that you found it useful. Good point about doing a send-to on a single file, I can say that I had not considered it 🙂

  3. Fantastic beat ! I wish to apprentice while you amend your site, how can i subscribe for a blog website?
    The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright
    clear idea

  4. Link exchange іs nothing еlse еxcept it
    is simply placing thе otheг person’s webpage link on уour pаge
    аt proper plаce and other person will alѕo do sаme
    in favor of уou.

  5. Simply desire to say your article is as amazing.
    The clarity in your post is simply excellent and i can assume you’re an expert
    on this subject. Fine with your permission let me to grab your RSS feed to keep up to date with forthcoming post.

    Thanks a million and please carry on the rewarding
    work.

Leave a Reply

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