Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read specific words from text file 1

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
Hi ... I have a data text file that contains some basic information on each line. I am wondering if there is a way to read the lines of the text file and search for a line containing a word (in my case "FINISH") and also finds todays date on the same line and returns the results as a number and displays a message box like: "Found 4 Lines" The text file looks like this:

Code:
START RUN: 1/11/2013 1:21:47 PM  C:\Mach3\GCode\8-736604-01\8-736604-01 r1.txt
FINISH RUN: 1/11/2013 1:21:49 PM  C:\Mach3\GCode\8-736604-01\8-736604-01 r1.txt
START RUN: 1/11/2013 2:08:59 PM  C:\Mach3\GCode\8-736604-01\8-736604-01 r1.txt
FINISH RUN: 1/11/2013 2:09:02 PM  C:\Mach3\GCode\8-736604-01\8-736604-01 r1.txt
START RUN: 1/11/2013 2:21:52 PM  C:\Mach3\GCode\8-736604-01\8-736604-01 r1.txt
FINISH RUN: 1/11/2013 2:21:55 PM  C:\Mach3\GCode\8-736604-01\8-736604-01 r1.txt
START RUN: 1/11/2013 2:52:11 PM  C:\Mach3\GCode\8-736604-01\8-736604-01 r1.txt
FINISH RUN: 1/11/2013 2:52:11 PM  C:\Mach3\GCode\8-736604-01\8-736604-01 r1.txt
 
I was able to find some info on this and got to this point. It seems to work pretty good, but I am not sure if this is how it should be written or is the most efficient way. If someone could look it over for me, I would greatly appreciate it.

Code:
Const ForReading = 1

Dim strSearchFor, strSearchWrd, LineCount, objFSO, objTextFile, arrLines
strSearchFor = Date
strSearchWrd = "FINISH"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile("c:\scripts\LogFile.txt", ForReading)

LineCount = 0
do until objTextFile.AtEndOfStream

    strLine = objTextFile.ReadLine()

    If InStr(strLine, strSearchFor) <> 0 then
	If InStr(strLine, strSearchWrd) <> 0 then

   arrLines = Split(strLine,vbCrLf)
   LineCount = LineCount + 1

	End If
	End If
   
loop
wscript.echo "Count for Today:  " & LineCount
objTextFile.Close
 
This line seems useless:
arrLines = Split(strLine,vbCrLf)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks, PHV

I was a bit confused on these lines

Code:
 strLine = objTextFile.ReadLine()
  If InStr(strLine, strSearchFor) <> 0 then
  If InStr(strLine, strSearchWrd) <> 0 then

Since I needed to search for two items, would this be the correct way to do that? Two "IF" statements?
 
If you prefer a single If:
If InStr(strLine, strSearchFor) <> 0 And InStr(strLine, strSearchWrd) <> 0 Then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top