View Complete Thread
  • Re: Compress and archive IIS Logs

    Aug 31, 2006 06:50 PM|kryan762|LINK

    Below is a vbs script we use.  Not sure if we found it on the net or someone from the team wrote it.  Works very well.  Does require some sort of commandline based zip program.  We use gzip which is availiable free online.   Hope this is what you are looking for.  

    Option Explicit

    Const bDEBUG = False
    Const zipProgram = "C:\gzip.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 5 days old will be zipped."
    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 > 5 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