Tuesday, February 10, 2015

Minifying JS in Windows easily with Ajaxmin BATCH script

Minifying js and css file has been a standard practice to reduce the webpage size.
Various tools are created to tackle the problem, especially on Mac OS X that includes batch processing capabilities.

However, it is a pain to do so in Windows without installing 3rd party extension into IDE.

This motivate me to write a simple script that can batch processing the js and easy to use.

This is a batch script that minify js and css files using AjaxMin
The script will scan current folder and its sub folder.
It will save the minified file with .min.js or .min.css

This batch script required AjaxMin to run, install AjaxMin into its default folder
C:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier

@ECHO off
set PATH="C:\Program Files (x86)\Microsoft\Microsoft Ajax Minifier\";%PATH%
setlocal enabledelayedexpansion
for /f %%f in ('dir /b /s^|findstr /r ".*\.js$ .*\.css$"') do (
set "N=%%f"
if "!N:.min%%~xf=!"=="!N!" (
@ajaxmin -clobber %%f -o %%~npf.min%%~xf
)
)
pause
save the script as .bat file
copy it to your /js folder and run it.

Feel free to copy and modify.

No comments:

Post a Comment