I generally like to keep the current months web logs around for convenience, and I’m not the kind of IT guy who likes to delete log files.. no waste disk space.. so I use this script to accomplish the goal. Yes, this could be better written, but for now, it serves my goals of archiving my weblogs to individual .zip files (I hate super huge zip files, as it can be tedious to extract a single file).
I set the variables at the top, then configure this as a scheduled task to run monthly. Voila, all log files from previous months get automatically archived to individual zip files for me. Lots of disk space savings (Especially on large log files)
Option Explicit
Const ForWriting = 2
'on error resume next
Dim objFSO, objTxt, intMonth, intYear, filenam, objNewZipFolder, THISintMonth
Dim objApp, myFolder, objItem, objItem2, myZipFile, found, logprefix
'************************** CHANGE THESE VARIABLES ********************************
logprefix = "ex"
myFolder = "D:\inetpub\website\W3SVC1002222222"
'**********************************************************************************
' Then run this script from a cscript dos window (cscript scriptname.vbs) and Voila.. Zip's
intMonth = Month(Date)
THISintMonth = Month(Date)
intYear = Year(Date)
If intMonth = 1 Then
intMonth = 12
intYear = intYear - 1
Else
intMonth = intMonth - 1
End If
If intMonth < 10 Then
intMonth = "0" & intMonth
End If
if THISintMonth < 10 Then
THISintMonth = "0" & THISintMonth
end if
intYear = right(intYear,2)
' Create a Shell object
Set objApp = CreateObject( "Shell.Application" )
For Each objItem in objApp.NameSpace( myFolder ).Items
if instr(objItem.Name,"ex" & intYear) and instr(objItem.Name,".log") and not instr(objItem.Name,"ex" & intYear & THISintMonth) then
filenam = objItem.name
filenam = replace(filenam,".log","")
'wscript.echo filenam
myZipFile = myFolder & "\" & filenam & ".zip"
' Create an empty ZIP file
Set objFSO = CreateObject( "Scripting.FileSystemObject" )
Set objTxt = objFSO.OpenTextFile( myZipFile, ForWriting, True )
objTxt.Write "PK" & Chr(5) & Chr(6) & String( 18, Chr(0) )
objTxt.Close
WScript.Sleep 100
Set objTxt = Nothing
Set objNewZipFolder = objApp.NameSpace( myZipFile )
objNewZipFolder.copyHere objItem
wscript.echo "Working on " & myFolder & "\" & filenam & ".zip"
found=false
do until found=true
WScript.Sleep 10000
if objFSO.FileExists(myFolder & "\" & filenam & ".zip") then
WScript.Sleep 5000
if objApp.NameSpace( myZipFile ).Items.Count > 0 then
found=true
end if
else
'wscript.echo "NOT FOUND YET"
end if
Loop
wscript.echo "DELETING " & objItem.name & " NOW"
on error resume next
WScript.Sleep 10000
objFSO.DeleteFile myFolder & "\" & objItem.name
if err.number > 0 then
wscript.echo "error DELETING - in use, trying " & objItem.name & " again in 30 seconds"
WScript.Sleep 30000
objFSO.DeleteFile myFolder & "\" & objItem.name
end if
on error goto 0
end if
Next
Note: I TAKE VIRTUALLY NO CREDIT ON THIS SCRIPT.. I HACKED IT TOGETHER FROM NUMEROUS ONLINE SOURCES AND JUST DEBUGGED IT TO WORK WELL FOR ME (I added a few timer sleep functions to ensure the zip was completed before continuing, not the most optimized, but it works for me with log files < 1G/day..). USE AT YOUR OWN RISK