Tuesday, October 17, 2006

Determine Current IIS Log File with VBScript

It is common place to have IIS generate log files that are pretty huge. So frequently there has been a need for us to copy off the log files to a remote storage location and cleanup the local log directory so that the server doesn't stop logging IIS traffic.

This seems like a pretty straight forward task except that we can't copy off and delete the current working log file because, obviously, it's still being written to by IIS. So, in order to get around this little dilemma it's been desirable to figure out what IIS log file is currently in use.

What makes it a little more challenging is that IIS log file names are generated in GMT (Greenwich Mean Time). Which is handy for standards, but makes progmatically determining what file is in use, daunting for a novice. But, like many tasks this, too, is a job that can be handled quite easily by a little VBScript.


Function CurrentIISLogFile(vLogNameFormat)
  'This script is provided under the Creative Commons license located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for commercial purposes with out the expressed written consent
  'of NateRice.com

  '
  ' 1) Hourly -     exyymmddhh.log
  ' 2) Daily -      exyymmdd.log
  ' 3) Weekly -     exyymmww.log
  ' 4) Monthly -    exyymm.log
  '
  'example:
  '
  ' Wscript.Echo CurrentIISLogFile(1)
  '  Returns: ex06101716.log
  '
  'This script is provided under the Creative Commons liscense located
  'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not
  'be used for comercial purposes with out the expressed written consent
  'of NateRice.com


  'Here we find the current date and time,
  'we then add X hours to it to compensate for
  'GMT, all log files are time stamped in GMT.
  set oShell = CreateObject("WScript.Shell")
  vGMTDiff = oShell.RegRead("HKEY_LOCAL_MACHINE\System\CurrentControlSet\" & _ 
        "Control\TimeZoneInformation\ActiveTimeBias")
  vGMTDiff = (vGMTDiff)/60
  sTimeAndDate = DateAdd("h", vGMTDiff, Now)

  'Now we split the values into 3 strings: date, time, AM/PM
  aDateAndTime = Split(sTimeAndDate)

  sDate = aDateAndTime(0)
  sTime = aDateAndTime(1)
  sAMPM = trim(aDateAndTime(2))

  'now split up the date's and times so we can get relevent
  'info like month, day, and year
  aTime = Split(sTime,":")
  aDate = Split(sDate,"/")

  sHour = aTime(0)
  sMonth = aDate(0)
  sDay = aDate(1)
  sYear = right(aDate(2),2)
  sWeek = DatePart("ww", sDate)

  'convert hours to 24 hour format
  if trim(sAMPM) = "PM" Then sHour = sHour + 12
 
  'here we append a "0" to values less than 10
  if len(sMonth) = 1 Then    sMonth = "0" & sMonth
  if len(sDay) = 1 Then sDay = "0" & sDay
  if len(sHour) = 1 Then sHour = "0" & sHour
  If len(sWeek) = 1 Then sWeek = "0" & sWeek

  'now return the appropriate string
  If vLogNameFormat = 1 Then
    CurrentIISLogFile = "ex" & sYear & sMonth & sDay & sHour & ".log"
  Elseif vLogNameFormat = 2 Then
    CurrentIISLogFile = "ex" & sYear & sMonth & sDay & ".log"
  Elseif vLogNameFormat = 3 Then
    CurrentIISLogFile = "ex" & sYear & sMonth & sWeek & ".log"
  Else
    CurrentIISLogFile = "ex" & sYear & sMonth & ".log"
  End If
End Function
Send this to:                          

Comments

Name
URL
Email
Email address is not published
Remember Me
Comments

CAPTCHA
Write the characters in the image above