Friday, February 11, 2011

Merging Sources with Help of Batch Scripting

Batch files are exteremely useful for programmers. Developers can use batch scripting to automate boring jobs like making builds and packaging software. And not only for this - for example, to merge the existing dozens of script files to the single one. Here is the example of script that will help you to merge many JavaScript files placed in the same folder into the single script file. You can easily change this script with the help of Dr.Batcher, an advanced batch files editor.
@echo off
SET NewFile=cpfunct.js
cd \cpfunct\
@echo.>%NewFile%
for %%i in (*.js) do COPY /B %NewFile%+%%i %NewFile%
move %NewFile% ..\%NewFile%

It is useful to store the current version of the resulting JS in top of the file. Here is code that automatically reads and increments the version of the script before merging all files:

@echo off
setlocal
set /p vers=
for /f "tokens=2 delims==" %%i in ("%vers%") do set /a ver=%%i+1
1>main.tmp (
echo var vers=%ver%
more +1 result.js
)
del result.js
for %%i in (*.js) do copy /b main.tmp+%%i main.tmp
ren main.tmp result.js

Have any ideas about further enhancements? Don't hesitate to post your comments or send your suggestions to us!

No comments:

Post a Comment

Translate