Reading log details from text file

PATEL Prashant Kumar 40 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

Microsoft 365 and Office | Development | Other
Microsoft 365 and Office | Excel | For business | Windows
Developer technologies | VB
{count} votes

Answer accepted by question author
  1. MotoX80 37,156 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 
    
    2 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

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