Solved

getting logs based on date

Posted on 2009-06-29
6
171 Views
Last Modified: 2012-05-07
I am looking for a VB6 script in which I will open event.log file and based on todays date will grab the logs.

Here is my event.log file
sometext, 6/26/2009, sometext
sometext, 6/27/2009, sometext
sometext, 6/27/2009, sometext
sometext, 6/27/2009, sometext
sometext, 6/27/2009, sometext
sometext, 6/27/2009, sometext
sometext, 6/29/2009, sometext
sometext, 6/29/2009, sometext

So in this example I only want to grab last two lines and email to myself

I know how the email part will work. just need help in passing last two lines to a variable.

0
Comment
Question by:CalmSoul
  • 3
  • 3
6 Comments
 
LVL 3

Expert Comment

by:astroviper
ID: 24738097
Note I had to manually force the date to mm/dd/yyyy as my OS is set to dd/mm/yyyy and I couldn't find an easier way around it.

' source file

strInput  = "event.log"
 

' use today's date

strDate = Month(Date) & "/" & Day(Date) & "/" & Year(Date)
 

WScript.Echo GetLines(strInput, strDate)
 

Function GetLines(input, sDate)

    seperator = VbCrLf

    Const ForReading = 1

    output = ""
 

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objInput = objFSO.OpenTextFile(strInput, ForReading)

    Do Until objInput.AtEndOfStream

        strLine = objInput.Readline

        If InStr(strLine, sDate) Then output = output & strLine & seperator

    Loop

    objInput.Close

    GetLines = output

End Function

Open in new window

0
 
LVL 5

Author Comment

by:CalmSoul
ID: 24738949
where the output is going?
0
 
LVL 3

Expert Comment

by:astroviper
ID: 24741167
I was hoping you'd be able to tell me :p. If you can provide an example of this email part you know how to get working then I can have a look at joining them up.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Author Comment

by:CalmSoul
ID: 24741424
here is the one which sends the email out
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.GetFile("myTextFile.TXT")

If objFile.Size > 0 Then

    Set objReadFile = objFSO.OpenTextFile("myTextFile.TXT", 1)

    strFile= objReadFile.ReadAll

    objReadFile.Close

    Set objReadFile = Nothing

    cPos = InstrRev(strFile, vbNewLine & "###")

    If cPos>0 Then

      cPos=cPos+2 'To not include the preceeding carriage return

    Else

      cPos=1

    End If

 

    strLastLines = Mid(strFile, cpos)

Else

End If

 

Set objFSO = Nothing

Set objFile = Nothing

 

If strLastLines <> "" Then

  Set objEmail = CreateObject("CDO.Message")

  Set cdoConfig = CreateObject("CDO.Configuration")

  sch = "http://schemas.microsoft.com/cdo/configuration/" 

  cdoConfig.Fields.Item(sch & "sendusing") = 2

  cdoConfig.Fields.Item(sch & "smtpserver") = "SMTP server" 

  cdoConfig.Fields.update 

  Set objEmail.Configuration = cdoConfig 

  objEmail.From = "sender address"

  objEmail.To = "recipient"

  objEmail.Subject = "auto send email thinggy" 

  objEmail.Textbody = strLastLines 

  objEmail.Send

  Set objEmail = Nothing

  Set cdoConfig = Nothing

End If

Open in new window

0
 
LVL 5

Author Comment

by:CalmSoul
ID: 24741426
Is it possible if I read date from another text file and then based on that date get logs? instead of todays date?
0
 
LVL 3

Accepted Solution

by:
astroviper earned 500 total points
ID: 24741514
Yea that's easy, just read in the file and assign it to strDate instead of the actual date.

What's in the myTextFile.TXT?

To email the output, all you'd need to do is add the output from my function into that message body, note line 35. Just make sure you have the correct file and date assigned to those variables first.

If strLastLines <> "" Then

  Set objEmail = CreateObject("CDO.Message")

  Set cdoConfig = CreateObject("CDO.Configuration")

  sch = "http://schemas.microsoft.com/cdo/configuration/" 

  cdoConfig.Fields.Item(sch & "sendusing") = 2

  cdoConfig.Fields.Item(sch & "smtpserver") = "SMTP server" 

  cdoConfig.Fields.update 

  Set objEmail.Configuration = cdoConfig 

  objEmail.From = "sender address"

  objEmail.To = "recipient"

  objEmail.Subject = "auto send email thinggy" 

  objEmail.Textbody = strLastLines & VbCrLf & GetLines(strInput, strDate)

  objEmail.Send

  Set objEmail = Nothing

  Set cdoConfig = Nothing

End If
 

Function GetLines(input, sDate)

    seperator = VbCrLf

    Const ForReading = 1

    output = ""

 

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set objInput = objFSO.OpenTextFile(strInput, ForReading)

    Do Until objInput.AtEndOfStream

        strLine = objInput.Readline

        If InStr(strLine, sDate) Then output = output & strLine & seperator

    Loop

    objInput.Close

    GetLines = output

End Function

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Not needed 13 95
Rubik's Cube Code for Effective Presentation 3 41
egit plugin on eclipse 8 41
How to obtain Administrator permission when you are the Administrator 6 66
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now