Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


finding multiple strings in a file

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,59576
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> finding multiple strings in a file
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 finding multiple strings in a file - 4/28/2008 1:28:59 AM   
  elinenbe

 

Posts: 10
Score: 0
Joined: 4/24/2008
Status: offline
What I'd like to do is find multiple strings in a file, and find out how many times each occur.  What I can't figure out is an elegant way to do this.

Here is my file
C:\test.txt

Contents of the file
Eric
Mark
John
Eric
Eric
Mark
Dave
Dave
Mark
Mike
Mike
Eric

So far I have:

   Const ForReading = 1

   Set objRegEx = CreateObject("VBScript.RegExp")
   objRegEx.Pattern = "Eric"

   Set objFSO = CreateObject("Scripting.FileSystemObject")
   Set objFile = objFSO.OpenTextFile("C:\test.txt", ForReading)

   Do Until objFile.AtEndOfStream
       strSearchString = objFile.ReadLine
       Set colMatches = objRegEx.Execute(strSearchString) 
       If colMatches.Count > 0 Then
           For Each strMatch in colMatches  
               Wscript.Echo strSearchString
           Next
       End If
   Loop
   objFile.Close

I could do this for each of the names and create a counter for each name, so at the end I get something like

Eric: 4
Mark: 3
Dave: 2
Mike: 2
John: 1

Although I'd love to use some arrays to simply everything into a single loop.  Though I can't figure out how to make it work.

Thanks,
Eric
 
 
Post #: 1
 
 RE: finding multiple strings in a file - 4/28/2008 1:43:49 AM   
  ebgreen


Posts: 4595
Score: 29
Joined: 7/12/2005
Status: offline
If all you want is counts, you don't need to loop at all. Look at this example:

Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
strFile = oFSO.OpenTextFile("C:\Test.txt").ReadAll()
Dim oRE : Set oRE = New Regexp
oRE.Global = True
oRE.Pattern = "Eric"
Set colMatches = oRE.Execute(strFile)
WScript.Echo "Eric = " & colMatches.Count

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to elinenbe)
 
 
Post #: 2
 
 RE: finding multiple strings in a file - 4/28/2008 1:50:45 AM   
  elinenbe

 

Posts: 10
Score: 0
Joined: 4/24/2008
Status: offline
How would I go about doing that for numerous strings?  So, I could do it with:

Eric
Dave
Mike
Mark
John

Thanks,
Eric

(in reply to ebgreen)
 
 
Post #: 3
 
 RE: finding multiple strings in a file - 4/28/2008 1:52:20 AM   
  dm_4ever


Posts: 2174
Score: 32
Joined: 6/29/2006
From: Orange County, California
Status: offline
Just change the pattern and do the same things using an array or something similar...

try...


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to elinenbe)
 
 
Post #: 4
 
 RE: finding multiple strings in a file - 4/28/2008 1:53:07 AM   
  ebgreen


Posts: 4595
Score: 29
Joined: 7/12/2005
Status: offline
Make an array of the names:

arrNames = Array("Eric", "Dave", ...)

Then Loop through the array checking for each name:

For Each strName In arrNames
   oRE.Pattern = strName
   .
   .
   .
   WScript.Echo strName & " = " & colMatches.Count
Next

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to elinenbe)
 
 
Post #: 5
 
 RE: finding multiple strings in a file - 4/28/2008 2:02:36 AM   
  elinenbe

 

Posts: 10
Score: 0
Joined: 4/24/2008
Status: offline
Thanks.  You've been a great help.

I took care of it like this:

Function GetExceptionsInfo()

   strPatterns = Array("Mark", _
                       "Eric", _
                       "Mike", _
                       "Dave")
   Dim ExceptionsArray(3)

   For ExceptionsCheck = 0 to 3       
       Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
       strFile = oFSO.OpenTextFile("C:\test.txt").ReadAll()
       Dim oRE : Set oRE = New Regexp
       oRE.Global = True
       oRE.Pattern = strPatterns(ExceptionsCheck)
       Set colMatches = oRE.Execute(strFile)
       WScript.Echo strPatterns(ExceptionsCheck) & "=" & colMatches.Count
   Next

(in reply to ebgreen)
 
 
Post #: 6
 
 RE: finding multiple strings in a file - 4/28/2008 4:09:10 AM   
  ehvbs

 

Posts: 2012
Score: 48
Joined: 6/22/2005
From: Germany
Status: offline
Hi elinenbe,

using a dictionary would make your script much easier and more efficient:


      

output (on your test data):

4 Eric
3 Mark
1 John
2 Dave
2 Mike

Regards

ehvbs

(in reply to elinenbe)
 
 
Post #: 7
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> finding multiple strings in a file Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts