We help IT Professionals succeed at work.

Can VB look for a phrase in a logfile?

WeberTech
WeberTech asked
on
Can VB check a logfile for a certain phrase like, "Job Completed"?  Sorry, I'm a beginner at this.
Comment
Watch Question

Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Sure, here's the basic idea, just add what ever logic you desire whre the MsgBox is.

strFileName = "C:\temp\file.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, 1, False)
strText = objFile.ReadAll
objFile.Close
If Instr(strText, "Job Completed") > 0 Then
   MsgBox "Found matching text"
End If

Open in new window

~bp
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Or, if it's a particularly large file, you probably won't want to read the entire contents into memory, so you could read the file line by line.

Rob.
strFileName = "C:\temp\file.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, 1, False)
blnFound = False
strTextToFind = "Job Completed"
Do Until objFile.AtEndOfStream
   strLine = objFile.ReadLine
   If Instr(strLine, strTextToFind) > 0 Then
      blnFound = True
      Exit Do
   End If
Loop
If blnFound = True Then
   MsgBox strTextToFind & " was found."
Else
   MsgBox strTextToFind & " was not found."
End If

Open in new window

Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
That all being said, depending on your ultimate need, you might be able to take advantage of the built in FIND or FINDSTR command from a BAT file to look for the matching string, they are easy to use and very fast.

~bp

Author

Commented:
Well, I worked the code into my script and it seems to work quickly and gives me the results I need.  I appreciate your help!

Author

Commented:
You guys rock!  Thank you!
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome, thanks for the feedback.

~bp

Explore More ContentExplore courses, solutions, and other research materials related to this topic.