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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Searching for specific word in a text file 1

Status
Not open for further replies.

djhawthorn

Technical User
Mar 4, 2002
641
AU
How would I go about searching for a specific word in a text file? At the moment I have this function:

Function LogFileSearch (LogFileLocation, SearchString)
TempChars = ""
SearchStringFound = false

If not WshFileSys.FileExists(LogFileLocation) Then
Msgbox "Could not open the log file " & LogFileLocation & ". Script will now exit.", VBOKonly + VBCritical, "Log File Search"
Wscript.Quit
End If

Set LogFile = WshFileSys.OpenTextFile(LogFileLocation)

Do While ((LogFile.AtEndOfStream <> True) and (TempChars <> SearchString))
TempChars = LogFile.Read(len(SearchString))

If TempChars = SearchString Then
SearchStringFound = true
End If

LogFile.SkipLine
Loop

LogFile.Close

LogFileSearch = SearchStringFound
End Function

Which sort of works, but is not quite functional (it only searches the first x characters in the text file from every line).

What I need is to search every word (words seperated by the space character) in every line, and when (if) it finds a match, it will return true. Can someone improve the above script, or point me in the right direction?
 
You probably want to do this with reg exps :)

Have a look here: thread329-632979

You have the right line-by-line approach though.

Here's a great reference to vbscript reg exps:

Let us know what you try we can certainly help if you're stuck with the actual reg exp.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Ok, before I look at regex's, I found this snippet of code:

Code:
Do Until objTextFile.AtEndOfStream
  strLine = objTextFile.ReadLine

  If InStr(strLine, StrSearchString) <> 0 Then
    WScript.echo strLine
  End If
Loop

Which works for looking for the string, but I have the problem where this will match anything. For example, if I had a log file containing either of

Error - the file something.txt was not found

or

The file C:\winnt\error.log was backed up

The above code would match both times. I need it to NOT pickup lines that have &quot;error&quot; in it unless &quot;error&quot; is a word on its own.


/me trundles off to look at regex's.
 
Yes reg exps are your way forward - look at this snippet (in ASP, i'm sure you can change it to wsh):
Code:
Dim RegX, Match, matchesColl, sPattern, sString

sPattern = &quot;\b(\w+?)\b&quot;
sString = &quot;Every word in this sentence.&quot; & vbCrLf & &quot;This one's on a new line.&quot;

Set RegX = NEW RegExp
RegX.Pattern = sPattern
RegX.Global = True
RegX.IgnoreCase = True
RegX.MultiLine = True

Set matchesColl = RegX.Execute(sString)

Response.write(&quot;Searching: &quot; & sString & &quot;<br>&quot;)
For Each Match in matchesColl
  Response.Write(&quot;[&quot; & Match & &quot;]&quot;)
Next

Combined with line-by-line file reading, some twiddling of the pattern should get u what you want.

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Have a look at this:
Code:
Dim RegX, Match, matchesColl, sPattern, sString

'sPattern = &quot;\b(\w+?)\b&quot; 'this pattern matches all words
sPattern = &quot;\b(Error)\b[^.]&quot; 'this pattern matches &quot;error&quot; when there isn't a . after it
sString = &quot;Error - the file something.txt was not found&quot; & vbCrLf & &quot;The file C:\winnt\error.log was backed up&quot;

Set RegX = NEW RegExp
RegX.Pattern = sPattern
RegX.Global = True
RegX.IgnoreCase = True
RegX.MultiLine = True

Set matchesColl = RegX.Execute(sString)

Response.write(&quot;Searching: &quot; & sString & &quot;<br>&quot;)
For Each Match in matchesColl
  Response.Write(&quot;[&quot; & Match.subMatches(0) & &quot;]&quot;)
Next

You should be able to loop through line by line and match stuff this way :)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top