Wednesday, August 20, 2008

Batch files - Delete 7 days older back up files

Its really difficult to calculate date in batch files since date object is not available. Please Comment if anyone has got suggestion about below code



set Month=%date:~3,2%
set Day=%date:~0,2%
set Year=%date:~6,4%
Set CDay=%date:~0,2%
Set CMonth=%date:~3,2%
Set CYear=%date:~6,4%

if 01 EQU %Day% set Day=1
if 02 EQU %Day% set Day=2
if 03 EQU %Day% set Day=3
if 04 EQU %Day% set Day=4
if 05 EQU %Day% set Day=5
if 06 EQU %Day% set Day=6
if 07 EQU %Day% set Day=7
if 08 EQU %Day% set Day=8
if 09 EQU %Day% set Day=9


if 01 EQU %Month% set Month=1
if 02 EQU %Month% set Month=2
if 03 EQU %Month% set Month=3
if 04 EQU %Month% set Month=4
if 05 EQU %Month% set Month=5
if 06 EQU %Month% set Month=6
if 07 EQU %Month% set Month=7
if 08 EQU %Month% set Month=8
if 09 EQU %Month% set Month=9

set Lday=%Day%
set /A LMonth=%Month%

if %CDay% EQU 02 set /A Lday -= 8
if %CDay% EQU 03 set /A Lday -= 8
if %CDay% EQU 04 set /A Lday -= 8
if %CDay% EQU 05 set /A Lday -= 8
if %CDay% EQU 06 set /A Lday -= 8
if %CDay% EQU 07 set /A Lday -= 8
if %CDay% EQU 08 set /A Lday -= 8
if %CDay% EQU 01 set /A Lday -= 7
if %CDay% GEQ 09 set /A Lday -= 7



if %Day% LEQ 8 GOTO Mday
Goto BackP

:Mday
if %LMonth% EQU 1 Goto :Jan
if %LMonth% EQU 2 Goto :Feb
if %LMonth% EQU 3 Goto :Mar
if %LMonth% EQU 4 Goto :Apr
if %LMonth% EQU 5 Goto :May
if %LMonth% EQU 6 Goto :Jun
if %LMonth% EQU 7 Goto :Jul
if %LMonth% EQU 8 Goto :Aug
if %LMonth% EQU 9 Goto :Sep
if %LMonth% EQU 10 Goto :Oct
if %LMonth% EQU 11 Goto :Nov
if %LMonth% EQU 12 Goto :Dec

:Jan
if %Day% LEQ 8 goto PrevYear
set /A Lday += 31
set /A LMonth = 12
Goto Backp

:Feb
set /A Lday += 31
set /A LMonth = 1
Goto Backp

REM ***********************
REM Check for leap year
REM ***********************

:Mar
if %Year% EQU 2004 set /A Lday += 29
if %Year% EQU 2008 set /A Lday += 29
if %Year% EQU 2012 set /A Lday += 29
if %Year% EQU 2016 set /A Lday += 29
else set /A Lday += 28
set /A LMonth = 2

:Apr
set /A Lday += 31
set /A LMonth = 3
Goto Backp

:May
set /A Lday += 30
set /A LMonth = 4
Goto Backp

:Jun
set /A Lday += 31
set /A LMonth = 5
Goto Backp

:Jul
set /A Lday += 31
set /A LMonth = 6
Goto Backp

:Aug
set /A Lday += 31
set /A LMonth = 7
Goto Backp

:Sep
set /A Lday += 31
set /A LMonth = 8
Goto Backp

:Oct
set /A Lday += 30
set /A LMonth = 9
Goto Backp

:Nov
set /A Lday += 31
set /A LMonth = 10
Goto Backp

:Dec
set /A Lday += 30
set /A LMonth = 11
Goto Backp


REM ***********************
REM PrevYear is to take account of jan 1st previous day, which is dec 31st
REM ***********************

:PrevYear
set /A Year -= 1
set /A LDay += 31
set /A LMonth = 12
goto Backp

REM ***********************
REM pad days and months with 0's to get correct format if day is 1-9, and month is 1-9
REM ***********************


:Backp


if %Lday% LEQ 9 set Nday=0%Lday%
if %Lday% GEQ 10 set Nday=%Lday%
if %LMonth% LEQ 9 set Nmonth=0%LMonth%
if %LMonth% GEQ 10 set Nmonth=%LMonth%

Set /A MoveDate= %CYear%%CMonth%%CDay%


if %CDay% EQU 01 goto FileMove
goto FileDel


:FileMove

MD C:\Batchf~1\"month end backups"\LMEWSLive_backup_%MoveDate%

move C:\Batchf~1\Backup\HBDocProd_backup_%MoveDate%0100.bak C:\Batchf~1\"month end backups"\LMEWSLive_backup_%MoveDate%
move C:\Batchf~1\Backup\LMEWSArchive_backup_%MoveDate%0100.bak C:\Batchf~1\"month end backups"\LMEWSLive_backup_%MoveDate%
move C:\Batchf~1\Backup\LMEWSLive_backup_%MoveDate%0100.bak C:\Batchf~1\"month end backups"\LMEWSLive_backup_%MoveDate%

pause

exit


:FileDel


set /A Deldate = %Year%%Nmonth%%Nday%

move C:\Batchf~1\Backup\HBDocProd_backup_%Deldate%0100.bak C:\Batchf~1\Delete
move C:\Batchf~1\Backup\LMEWSArchive_backup_%Deldate%0100.bak C:\Batchf~1\Delete
move C:\Batchf~1\Backup\LMEWSLive_backup_%Deldate%0100.bak C:\Batchf~1\Delete

pause
exit

No comments: