Batch Script to Archive files by month

Man I finally got a bit of time to write what I've desperatly need on just about every production server. Enjoy world.

  • Install http://www.7-zip.org/
  • Set the SRCDIR,DESTDIR, and then Schedule it.
@echo off
::cls%
Echo Archive my logs please...
SET ZIPAPP="C:\Program Files\7-Zip\7z.exe"
SET SRCDIR=C:\MyDirectoryFullOfCrappyLogFiles\*.Log
SET DESTDIR=E:\MyDirectoryForStoringNEWCrappyZippedLogs\

FOR %%F in (%SRCDIR%) do ( call:archivefile "%%~tF" "%%F")
ECHO BYE BYE
:archivefile
SET MD=%1

if NOT %DATE:~10,4%%Date:~4,2%%Date:~7,2%==%MD:~7,4%%MD:~1,2%%MD:~4,2% %ZIPAPP% u -tzip %DESTDIR%%MD:~7,4%%MD:~1,2%_Logs.zip %2 > nul
IF %ERRORLEVEL%==0 DEL %2



Comments :

1
Anonymous said...
on 

Hello,
Nice script.
I think you'd rather use the line
if NOT %DATE:~10,4%%Date:~4,2%%Date:~7,2%==%MD:~7,4%%MD:~1,2%%MD:~4,2% %ZIPAPP% u -tzip %DESTDIR%%MD:~7,4%_%MD:~4,2%_Logs.zip %2 > nul

(see the difference ? %MD:~4,2% instead of %MD:~1,2% to save by month and not by day number)