VbScript Compress all IIS logs Files


Scenario:

Compress IIS Logs files, for all websites in a Windows 2000 or 2003 Server.

You will need to download the GZIP file here and put it in this folder “c:\gzip”, with the name “gzip.exe”

Option EXPLICIT

Const zipProgram = “C:\gzip\gzip.exe -9”

Dim objFSO, objFolder, objF, objWS
Dim strYear, strDay, strMonth, strDate

Dim strServer, strServerType, strServerMetaType, objService

strDate  = Date()
strYear  = DatePart (“yyyy”, strDate)
strDay   = DatePart (“d”, strDate)
strMonth = DatePart (“m”, strDate)

strServer         = “localhost”
strServerType     = “Web”
strServerMetaType = “W3SVC”

    If WScript.Arguments.Length = 1 THEN
        strServerType = WScript.Arguments( 1 )
        IF UCASE( strServerType ) = “FTP” THEN
            strServerType = “Ftp”
            strServerMetaType = “MSFTPSVC”
        ELSE
            strServerType = “Web”
            strServerMetaType = “W3SVC”
        END IF
    End IF

On Error Resume Next

Set objService = GetObject( “IIS://” & strServer & “/” & strServerMetaType )

SUB DoDirectory (objFold)
 DIM objUserDir, objFile, iDiff

 FOR EACH objUserDir IN objFold.SubFolders
  DoDirectory (objUserDir)
 Next

 For Each objFile in objFold.Files
  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
        iResult = objWS.Run (strCommand, 0, “true”)
            If iResult <> 0 Then
             ‘ não faz nada
            Else
             objFile.Delete
            End If
       Else

       End If
  End If
 Next
End Sub

SUB EnumServersites( objService )
    DIM objServer, strBindings, objFolder, objF, objFSO1

    SET objFSO1 = CreateObject (“Scripting.FileSystemObject”)

    FOR EACH objServer IN objService
        IF objServer.Class = “IIs” & strServerType & “Server” THEN
        Set objFolder = objFSO1.GetFolder (objServer.LogFileDirectory)
        Set objWS  = CreateObject (“WScript.Shell”)
            For Each objF in objFolder.SubFolders
                DoDirectory (objF)
            Next
    ‘Set objFSO = Nothing
    ‘Set objWS  = Nothing               
       END IF

    NEXT
End Sub

EnumServersites objService

Hope this script can be useful, and don’t forget to save in .vbs format file.

Advertisements

About rodvars
Been working in IT Services/Consulting for the past 15 years. My main areas of work are planning, development, managing and administration System infrastructures focusing on optimizing user processes, enforcing business security, performance enhancements, high availabilty and infrastucture scalability.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: