Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2003
    Posts
    5

    Unanswered: Process the last line of a text file in an array?

    I have written a vbScript to read a log file into an array. Should it find certain information, it's been designed to display a message on that machine and send an email to an Exchange group. However, what I'm running into is that the service that actually writes/creates the log that is being read is also crapping out which means that the log may be out of date (during normal processing, it writes every five minutes). What I would like to do is add to this script the functionality to process the last line of the text file and probably using DateDiff, write code that will allow for something to happen if the time/date isn't within the last, say 10 minutes or so. The time calculation piece I'm good with, I just don' t know what, if any, code is that would make the script pay special attention to just that last line in the file.

    Any suggestions?

  2. #2
    Join Date
    Feb 2002
    Posts
    2,232
    Is this a fixed length file or do the lines vary ? How long are the files ? Have you looked at just testing for end of file ?

  3. #3
    Join Date
    Nov 2003
    Posts
    5
    No fixed length, the file size varies because the team that supports the application being monitored clears the file at different intervals over the course of the day.

    As for the end of the file, that might work since the information is in the last part of the array but I don't know the code for checking the end of the file, either...but if you could point me in that direction, that just might give me what I'm looking for.

    Thank you for your time, pal!

  4. #4
    Join Date
    Feb 2002
    Posts
    2,232
    What are you using to read the file (the filesystemobject?) ?

  5. #5
    Join Date
    Nov 2003
    Posts
    5
    Originally posted by rnealejr
    What are you using to read the file (the filesystemobject?) ?
    Yes, filesystemobject is what I am using.

  6. #6
    Join Date
    Feb 2002
    Posts
    2,232
    You can use the following:

    Dim fso As New FileSystemObject
    Dim ts As TextStream
    Dim strTemp as String
    'Open file.
    Set ts = fso.OpenTextFile(filename)
    'Loop while not at the end of the file.
    Do While Not ts.AtEndOfStream
    strTemp = ts.ReadLine
    Loop
    'use strTemp as needed
    'Close the file.
    ts.Close

  7. #7
    Join Date
    Nov 2003
    Posts
    5

    Lightbulb

    Originally posted by rnealejr
    You can use the following:

    Dim fso As New FileSystemObject
    Dim ts As TextStream
    Dim strTemp as String
    'Open file.
    Set ts = fso.OpenTextFile(filename)
    'Loop while not at the end of the file.
    Do While Not ts.AtEndOfStream
    strTemp = ts.ReadLine
    Loop
    'use strTemp as needed
    'Close the file.
    ts.Close
    Okay, so I'm obviously lost here...that being said, I have what you have given me and understand it to a degree, but I am uncertain how to incorporate that into what I already have (or would it be simpler to just create a new script altogether?) Anyway, here's what I have, can you give me some direction? I mean, you don't have to rewrite the thing for me (obviously) but if you could give me some pointers on where stuff needs to go within what I already have, I think that'd help out a whole bunch...here goes....!

    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile("d:\filedir\monitor.log")
    Do While objTextFile.AtEndOfStream <> True
    arrAMSMonitor = split(objTextFile.Readline, ";")
    LogDate = Mid(arrAMSMonitor(3),13,19)
    If arrAMSMonitor(0) = "MSPAvailability=down" And DateDiff("n", Now, LogDate) > -5 Then
    Wscript.Echo "A down event was detected at " & Time & ". " & vbCr & "The log file recorded this event at " & LogDate & "." & vbCr & "Please check \\SERVERNAME\servershare\monitor.log for more information."
    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = "user@domain.com"
    objEmail.To = "user@domain.com"
    objEmail.Subject = "SERVER Monitor Error Reported"
    objEmail.Textbody = "A down event was detected at " & LogDate & ". Please check \\SERVERNAME\servershare\monitor.log for more information." & vbCrLf & "NOTE: As this error may have been previously reported; please note the date/time of last occurrence."
    objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.state.ky.us"
    objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
    End If
    i = i + 1
    Loop
    objTextFile.Close

    You see, right now, it's looking for a keyword in the first part of the array that it's taking from this text file and reporting if it finds that info. In addition to this, what I'd like for it to do is look at the very last line of this text file, check the time and date against the current time and date and report if that time and date checked isn't within the last 10 minutes (I know I'm being repetitive, but I'd rather do that than have you help me get something I don't need).

    By the way, thanks for your time and help...I sincerely appreciate it.

  8. #8
    Join Date
    Feb 2002
    Posts
    2,232
    After the Loop statement, you can use the arrAMSMonitor and do the necessary validation since it will contain the last line of the file.

  9. #9
    Join Date
    Nov 2003
    Posts
    5

    NEW DIRECTIVE!!!

    Okay,

    It seems as though playing on the time factor with this script is still a little sketchy. That being said, I am now looking to simplify this thing, so I have yet another direction I'm considering taking this (if possible).

    Instead of using DateDiff and calculating the time factor, I'd like to remove those commands and replace them with code that reads the file as an array in the same fashion as it has been, but instead of analyzing the time and calculating, I just want it to look for the "down" information. If it is found, I'd like for the script to continue displaying and the output on the hosting PC as well as sending the email, but I'd like for that "down" to be changed to "ErrorReported" or something of similar nature. Basically, I'd like to know how to write to a file that's reading information into an array (or is that not possible).

    Thanks so much again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •