Reading log details from text file

PATEL Prashant Kumar 20 Reputation points
2024-09-27T12:27:20.6933333+00:00

Hi,
I have a application log files in text format and want to read start/end time of a particular job from text file and then put it into an excel file with VB Script.
Let me know if any further clarification or info. is required.

Thanks

Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,875 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,720 questions
{count} votes

1 answer

Sort by: Most helpful
  1. MotoX80 33,471 Reputation points
    2024-09-27T17:25:26.2333333+00:00

    VB Script is being deprecated. Powershell is a better scripting language.

    This should work as long as you have the sequence that you posted. If you have jobs that start but do not finish, or you run multiple jobs at a time and have multiple start/stop records intermixed with each other, then you would need to modify the code to account for that.

    $log = get-content c:\temp\x.txt 
    $Results = @()
    foreach ($rec in $log) {
        $data = $rec.split("]")                           # create array of TOD, user, and message 
        if ($rec.contains("Auto-running algorithm")) {    # job start 
            $starttime = $data[0].replace("[","").trim()
        }
        if ($rec.contains("finished executing")) {        # job end 
            $endtime = $data[0].replace("[","").trim()
            $user = $data[1].replace("[","").trim()
            $matches = [regex]::matches($data[2], "'([^']+)'")    # pick off  'job name' 
            $jobname = $matches.Value
            $results += [PSCustomObject]@{
                JobName = $jobname
                User = $user
                StartTime = $starttime
                EndTime = $endtime
            }
            # reset variables for next job. 
            $starttime = ""
            $endtime = ""
            $user = ""
            $jobname = "" 
        }
    }
    $Results
    $Results | Export-Excel -Path C:\temp\x.xls 
    
    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.