Count number of times a string is in a text file
Moderators: Dorian (MJT support), JRL
Count number of times a string is in a text file
I'm a newbie here, but I'm trying to use Readfile to count the number of times the word ON is in a text file, IF the number is >2, then do something. This is further complicated by the fact that the word NONE is in the file, so I really need to count ON, if you get my drift.
Any help?
Any help?
A couple of weeks ago I posted a sample that might assist you in your cause. See the post at:
http://www.mjtnet.com/usergroup/viewtopic.php?t=1712
In your case, use ON as the delimiter for the "separate" command to split up the results of "readfile". Notice that "returnvar_count" will be one greater than the number of occurances of ON.
Hope this helps,
Dick
http://www.mjtnet.com/usergroup/viewtopic.php?t=1712
In your case, use ON as the delimiter for the "separate" command to split up the results of "readfile". Notice that "returnvar_count" will be one greater than the number of occurances of ON.
Hope this helps,
Dick
Use regular expressions:
VBSTART
Function CountPatterns(patrn, strng)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(strng)
CountPatterns = Matches.Count
End Function
VBEND
Let>WordToCount=ON
Let>MyString=ON AND ON AND ON NONE
VBEval>CountPatterns("\b%WordToCount%\b","%MyString%"),numOns
In the example above numOns will equal three as there are three distinct instances of the word ON. Note that it ignores the ON inside NONE because that is not a separate word. The \b \b around the word to find tells it to look for distinct instances.
VBSTART
Function CountPatterns(patrn, strng)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(strng)
CountPatterns = Matches.Count
End Function
VBEND
Let>WordToCount=ON
Let>MyString=ON AND ON AND ON NONE
VBEval>CountPatterns("\b%WordToCount%\b","%MyString%"),numOns
In the example above numOns will equal three as there are three distinct instances of the word ON. Note that it ignores the ON inside NONE because that is not a separate word. The \b \b around the word to find tells it to look for distinct instances.
MJT Net Support
[email protected]
[email protected]
Support, that may be what I need. How can I have MyString contain an entire text file I tried this, but got an error:
VBSTART
Function CountPatterns(patrn, strng)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(strng)
CountPatterns = Matches.Count
End Function
VBEND
Let>WordToCount=ON
ReadFile>C:\Documents and Settings\miked\Desktop\TEST.TXT,MyString
//Let>MyString=ON AND ON AND ON NONE
VBEval>CountPatterns("\b%WordToCount%\b","%MyString%"),numOns
Message>%numOns%
VBSTART
Function CountPatterns(patrn, strng)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.Pattern = patrn
regEx.IgnoreCase = True
regEx.Global = True
Set Matches = regEx.Execute(strng)
CountPatterns = Matches.Count
End Function
VBEND
Let>WordToCount=ON
ReadFile>C:\Documents and Settings\miked\Desktop\TEST.TXT,MyString
//Let>MyString=ON AND ON AND ON NONE
VBEval>CountPatterns("\b%WordToCount%\b","%MyString%"),numOns
Message>%numOns%
//Remove CRLFs and Quotes
StringReplace>MyString,CRLF,,MyString
StringReplace>MyString,",,MyString
VBEval>CountPatterns("\b%WordToCount%\b","%MyString%"),numOns
StringReplace>MyString,CRLF,,MyString
StringReplace>MyString,",,MyString
VBEval>CountPatterns("\b%WordToCount%\b","%MyString%"),numOns
MJT Net Support
[email protected]
[email protected]