Compress Weblogs and Remove Them With VBScript
Originally published October 6, 2004
I needed this today, so I wrote it. I thought either the script itself, or the technique might be useful.
One of my webservers got to the point where the overall diskspace available fell below 20%. Weblogs are very compressible - about 90%. Since this webserver has several hundred websites, weblogs consume quite a bit of space. This script returned most of the space on the webserver.
I use the free zip program from http://www.info-zip.org and it works quite well.
We change the default weblog directory to C:\PublicWebLogs and share that as PublicWebLogs on each web server. This script takes that as a parameter (either locally as “C:\PublicWebLogs“ or remotely as “\\webserver\PublicWebLogs“ and then traverses each subdirectory and finds all files that have a name like “ex*.log“ and compresses those files to “ex*.log.zip“ and then removes the original file. Weblogs that are less than two days old are excluded.
Option Explicit
Const bDEBUG = True
Const zipProgram = "c:\bri\zip\zip.exe"
Dim objFSO, objFolder, objF, objWS
Dim strYear, strDay, strMonth, strDate
If WScript.Arguments.Count <> 1 Then
WScript.Echo "weblogs.vbs startdirectory"
WScript.Quit (1)
End If
strDate = Date()
strYear = DatePart ("yyyy", strDate)
strDay = DatePart ("d", strDate)
strMonth = DatePart ("m", strDate)
wscript.echo "Script run on date " & strDate
Wscript.echo "log files more than 2 days old will be zipped and removed."
wscript.echo " "
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objWS = CreateObject ("WScript.Shell")
Set objFolder = objFSO.GetFolder (wscript.arguments (0))
For Each objF in objFolder.SubFolders
If bDebug Then wscript.echo "Checking folder " & objF.Path
DoDirectory (objF)
Next
Set objFSO = Nothing
Set objWS = Nothing
Sub DoDirectory (objFold)
Dim objUserDir, objFile, iDiff
For Each objUserDir in objFold.SubFolders
DoDirectory (objUserDir)
Next
For Each objFile in objFold.Files
If bDebug Then Wscript.echo "Checking file: " & objFile.Path
If LCase (Right (objFile.Name, 4)) = ".log" and LCase (Left (objFile.Name, 2)) = "ex" Then
Dim strCheckString, strY, strM, strD, strCommand, iResult
strCheckString = Mid (objFile.Name, 3, 6)
strY = "20" & Left (strCheckString, 2)
strM = Mid (strCheckstring, 3, 2)
strD = Right (strCheckString, 2)
iDiff = DateDiff ("d", strM & "/" & strD & "/" & strY, strDate)
If iDiff > 2 Then
strCommand = zipProgram & " " & objFile.Path & ".zip " & objFile.Path
If bDebug Then wscript.echo vbTab & "zip it: " & strCommand
iResult = objWS.Run (strCommand, 0, "true")
If iResult <> 0 Then
If bDebug Then wscript.echo vbTab & "result = " & iResult
Else
objFile.Delete
End If
Else
If bDebug Then wscript.echo vbTab & "don't zip it"
End If
End If
Next
End Sub