' LOGFIX.VBS
' Arguments: logdate
'   logdate  Date part of logfile name -
'            eg 990304  reads from ex990304.log  and writes to an990304.log
' Description:
' Reads IIS Extended logfile, and adds date field to each entry.
' If the date rolls-over, increments the date field used.
'
' To run it, open a DOS prompt, CD to your logfile directory
' (CD \WINNT\SYSTEM32\LOGFILES\W3SVCxx  usually)
' and enter
' CSCRIPT LOGFIX.VBS 990303
' to fix EX990303.log. The script will create AN990303.log, so you still
' have the original if anything goes wrong. (make sure you have enough
' disk space).
'
' This will fix your Weekly log files, provided there's enough information
' to tell when the date changes. If there are any gaps of more than 24 hours,
' it will give wrong results!
'
' Created: 31/March/1999 by Aengus Lawlor ([email protected])
'
' Date          Modification                            Author
' ----------------------------------------------------------
' 6/Sep/00    Removed lowercasing of log entries       ALawlor

OPTION EXPLICIT

dim fs_in, file_in
dim fs_out, file_out
dim fields, ArgObj
dim linein, ldate, ll, time_field, ltime
dim count

' note: run with CSCRIPT to echo output to file!

' Get the Arguments object
Set ArgObj = WScript.Arguments

' Test to make sure there is at least one command line arg - the command
If ArgObj.Count < 1 Then
       WScript.Echo "Usage: [CSCRIPT] logfix.vbs <logfile date>"
       WScript.Echo "eg: CSCRIPT logfix.vbs 990304"
       WScript.Quit (1)
End If

    Set fs_in = CreateObject("Scripting.FileSystemObject")
    Set file_in = fs_in.OpenTextFile("ex" & ArgObj.Item(0) & ".log", 1)
    Set fs_out = CreateObject("Scripting.FileSystemObject")
    Set file_out = fs_out.OpenTextFile("an" & ArgObj.Item(0) & ".log", 2,1)

Do While file_in.AtEndOfStream <> True

 linein = file_in.ReadLine
 If len(linein) > 0 Then                ' not blank
   fields=split(linein)
   If Left(linein,1) = "#" Then

     Select Case lcase(fields(0))
       Case "#date:"
         ldate = fields(1)
         ltime = "00:00:00"
         wscript.echo ldate

       Case "#fields:"
         ' I should probably check to see if Date is already there!
         fields(0) = "#Fields: date"    ' modify first entry in fields array
         linein = join(fields, " ")     ' rebuild Fields string from fields array
         for ll = 1 to ubound(fields)   ' find Time field
           if lcase(fields(ll)) = "time" then time_field = ll - 1
         next

       Case Else
         ' just output any other # lines without modification
     end select

     file_out.WriteLine(linein)

   Else  ' not a header line - it's a regular log entry
     if hour(fields(time_field)) < hour(ltime) then
       ' increment date
         ldate = year(datevalue(ldate)+1) & "-" & month(datevalue(ldate)+1) & "-" & day(datevalue(ldate)+1)
         wscript.echo ldate
         file_out.WriteLine("#Date: " & ldate & " 00:00:00")
     end if

     file_out.WriteLine(ldate & " " & linein)
     ltime = fields(time_field)

   End If

   count=count+1
   if (count mod 1000) = 0 then wscript.echo count

 end if

Loop

wscript.echo "Total number of entries:", count

    file_in.Close
    Set file_in = nothing
    Set fs_in = nothing
    file_out.Close
    Set file_out = nothing
    Set fs_out = nothing