'==========================================================================================
'= Program:  WebTrends Logfile Fixer for IIS
'= FileName: ConvertLogFiles.vbs
'= Description: This script searches a directory for ex*.log files and converts them to
'=              ex*c.log files.  This conversion process simply inserts a date field if
'=              none is present.  This script was written to address compatibility issues
'=              with our IIS logfiles and WebTrends.
'=                  This script recurses through all subdirectories, so make sure that you
'=              Have adequate disk space for all related logfiles.
'= Syntax: cscript ConvertLogFiles.vbs
'= Written By: Joshua Jacobsen March 21st, 2006
'==========================================================================================

Set MyFileObject = Wscript.CreateObject("Scripting.FileSystemObject")
ScanFolder(".\")

'==========================================================================================
' Abstraction
'==========================================================================================
Sub ScanFolder(inputDirectoryName)
  '------------------------------------------------------------------------------------------
  ' This is the main program loop.  It goes through the current directory, and searches for
  ' unconverted logfiles, calls the ConvertFile function, and then recurses sub directories.
  '------------------------------------------------------------------------------------------
  Set InputFolder = MyFileObject.GetFolder(inputDirectoryName)

  Set InputFiles = InputFolder.Files
  For Each InputFile in InputFiles
    'wscript.echo "ScanFolder 001: " & InputFile.Name
    if (left(InputFile.name, 2) = "ex") AND (right(InputFile.Name,4) = ".log") AND Not(right(InputFile.Name,5) = "c.log") then
      outputFileName = left(InputFile.Name,instr(InputFile.Name,".")-1) & "c.log"
      'wscript.echo "ScanFolder 002: " & InputFile.Name
      'wscript.echo "ScanFolder 003: " & outputFileName
      ConvertFile (inputDirectoryName & InputFile.name), (inputDirectoryName & outputFileName)
	end if '(left(InputFile.name, 2) = "ex") AND (right(InputFile.Name,4) = ".log") AND Not(right(InputFile.Name,5) = "c.log")
  next 'Each InputFile in InputFiles

  Set InputSubFolders = InputFolder.SubFolders
  For each inputSubFolder in inputSubFolders
    ScanFolder(inputDirectoryName & inputSubFolder.name & "\")
  next 'each inputSubFolder in inputSubFolders
end sub

Sub ConvertFile(inputFileName, outputFileName)
  '------------------------------------------------------------------------------------------
  ' This is the logfile correction function.  It takes a logfile, checks it for
  '------------------------------------------------------------------------------------------
  AbortConversion = 0
  wscript.echo "ConvertFile 001: " & inputFileName
  wscript.echo "ConvertFile 002: " & outputFileName
  Set InputLogFile = MyFileObject.OpenTextFile(inputFileName)
  Set OutputLogFile = MyFileObject.CreateTextFile(outputFileName)
  while not(InputLogFile.AtEndOfStream)

    RowData = InputLogFile.ReadLine

    if left(RowData,1) = "#" then

      '------------------------------------------------------------------------------------------
      ' Check the Fields listed in the fields line of the logfile
      '------------------------------------------------------------------------------------------
      if left(RowData,7) = "#Fields" then

        '------------------------------------------------------------------------------------------
        ' If this logfile already has date information, set the "AbortConversion" flag, which will
		' just dump the input logfile straight to the output logfile with no correction.  If the
		' logfile does not have the datefield, add it.
        '------------------------------------------------------------------------------------------
        if instr(RowData,"date") > 0 then
          AbortConversion = 1
          OutputLogFile.WriteLine RowData
        else 'instr(RowData,"date") > 0
          OutputLogFile.WriteLine "#Fields: date" & mid(RowData,instr(RowData," "))
        end if 'instr(RowData,"date") > 0

      else 'left(RowData,7) = "#Fields"
        OutputLogFile.WriteLine RowData
      end if 'left(RowData,7) = "#Fields"

      '------------------------------------------------------------------------------------------
      ' Get the date from the date line of the logfile
      '------------------------------------------------------------------------------------------
      if left(RowData,5) = "#Date" then
        OutputDate = trim(mid(RowData,instr(RowData," ")))
        OutputDate = trim(left(OutputDate,instr(OutputDate," ")))
        OutputDateAsDate = ASPDate(OutputDate)
      end if 'left(RowData,5) = "#Date"

    else 'left(RowData,1) = "#"

      '------------------------------------------------------------------------------------------
      ' If the conversion is not aborted, insert the date.  Keep in mind Greenwich Mean Time,
	  ' and add a day if the time is before 05:59:59.  For some reason, 05:59:59 is already the
	  ' next day, or I would have compared to 06:00:00.  IIS jumps the gun 1 second.
      '------------------------------------------------------------------------------------------
	  if AbortConversion = 0 then
        OutputTime = trim(left(RowData,instr(RowData," ")))
        If (OutputTime < "05:59:59") then
          OutputLogFile.WriteLine LogDate(DateAdd("d",1,OutputDateAsDate)) & " " & RowData
        else '(OutputTime < "05:59:59")
          OutputLogFile.WriteLine OutputDate & " " & RowData
        end if '(OutputTime < "05:59:59")
      else 'AbortConversion = 0
        OutputLogFile.WriteLine RowData
      end if 'AbortConversion = 0
    end if 'left(RowData,1) = "#"

  wend 'not(InputLogFile.AtEndOfStream)
  InputLogFile.Close
  OutputLogFile.Close
end sub

Function ASPDate(inputString)
  '------------------------------------------------------------------------------------------
  ' Logfile dates are in a nonstandard format.  Parse them to output an ASP date.
  '------------------------------------------------------------------------------------------
  'wscript.echo "ASPDate 001: " & inputString
  OutputDateYear  = mid(inputString,1,4)
  OutputDateMonth = mid(inputString,6,2)
  OutputDateDay   = mid(inputString,9,2)
  'wscript.echo "ASPDate 002: " & OutputDateMonth & "/" & OutputDateDay & "/" & OutputDateYear
  ASPDate = cdate(OutputDateMonth & "/" & OutputDateDay & "/" & OutputDateYear)
end Function

Function LogDate(inputDate)
  '------------------------------------------------------------------------------------------
  ' Logfile dates are in a nonstandard format.  Parse ASP date to get a Logfile date.
  '------------------------------------------------------------------------------------------
  OutputDateYear  = DatePart("yyyy",inputDate)
  OutputDateMonth = DatePart("m",inputDate)
  OutputDateDay   = DatePart("d",inputDate)
  LogDate = OutputDateyear & "-" & OutputDateMonth & "-" & OutputDateDay
end function

