Submitted By: Anonymous Submission

Copies and archives IIS logs.

Visual Basic
Edit|Remove
'dimension variables
Dim varLogPath, varService, varArchive, varLogFile
Dim varYear, varMonth, varDay
Dim objFS
Dim var30Days
Dim strComputer


' ----------------------------------------------------------
' set up variables
strComputer = "LocalHost"

Set objIIS = GetObject("IIS://" & strComputer & "/W3SVC")

varLogPath = objIIS.LogFileDirectory
varService = "w3svc1\"
varArchive = "c:\LogArchive\"


' ----------------------------------------------------------
' get yesterday's date
varYesterday = DateAdd( "d", -1, Date() )


' ----------------------------------------------------------
' create a formatted log file name
' for yesterday's log file

' 1. then the 2-digit year
varYear = Right( DatePart( "yyyy", varYesterday), 2)

' 2. Now the month - make sure it's 2 digits!
varMonth = DatePart( "m", varYesterday )
If Len(varMonth) = 1 then
	varMonth = "0" & varMonth
End If

' 3. Now the day - make sure it's 2 digits!
varDay = DatePart( "d", varYesterday )
If Len(varDay) = 1 then
	varDay = "0" & varDay
End If

' 4. Complete the log file name
varLogFile = "ex" & varYear & varMonth & varDay & ".log"


' ----------------------------------------------------------
' Create a file system object
Set objFS = WScript.CreateObject("Scripting.FileSystemObject")


' ----------------------------------------------------------
' Copy the file to the archive path
objFS.CopyFile varLogPath & varService & varLogFile, varArchive & varLogFile


' ----------------------------------------------------------
' get date for 30 days ago
var30Days = DateAdd( "d", -30, Date() )


' ----------------------------------------------------------
' create a formatted log file name
' for 30-day-ago log file

' 1. then the 2-digit year
varYear = Right( DatePart( "yyyy", var30Days), 2)

' 2. Now the month - make sure it's 2 digits!
varMonth = DatePart( "m", var30Days )
If Len(varMonth) = 1 then
	varMonth = "0" & varMonth
End If

' 3. Now the day - make sure it's 2 digits!
varDay = DatePart( "d", var30Days )
If Len(varDay) = 1 then
	varDay = "0" & varDay
End If

' 4. Complete the log file name
varLogFile = "ex" & varYear & varMonth & varDay & ".log"


' ----------------------------------------------------------
' Delete the file from the archive path
objFS.DeleteFile varArchive & varLogFile