Thursday, August 13, 2009

Batch Programming

Posted by Aries Wandari

I need to compress .bak backup MSSQL database file and copy to another server weekly. One option of doing it is using batch command and run it by scheduler.
clip_image002
This batch programming below is capture current year month, and compressing file based on file name (the filename has month description in itself).
-------------
::Display command at screen
echo on
::variable definition
set yyyy=none
set mm=none
set yyyymm=none
::take year and month parameter
for /F "tokens=2 delims=/ " %%i in ('date /t') do set mm=%%i
for /F "tokens=3 delims=/ " %%i in ('date /t') do set yyyy=%%i
::fill year month parameter
set yyyymm=%yyyy%%mm%
::looping at criterialysized file list at directory (by year month) and creating compressed 7z file

set source=D:\BackUp
pushd "%source%"
for /f "skip=1 delims=" %%z in (' dir /b *%yyyymm%*.bak') do 7z a Z:\backup%yyyymm%.7z "%%z"
popd

0 comments:

Post a Comment