?
Solved

Help with vbs

Posted on 2011-09-03
3
Medium Priority
?
381 Views
Last Modified: 2012-05-12
I got a text file that looks like
aaa
bbb
ccc
aaa

Is it possible to have vbs to output the number of counts per word and then display the word that has the highest number of counts?
aaa = 2
bbb = 1
ccc = 1

The highest (number of counts) = aaa
0
Comment
Question by:kt2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
prashanthd earned 2000 total points
ID: 36478003
Try the following code. Set the input filepath.

Incase there are multiple with same highest count, it will display all those words.

regards
Prashanth

input_file="C:\input.txt"

Set re = CreateObject("VBScript.RegExp")
Set oFileObj = CreateObject("Scripting.FileSystemObject")
Set oTxtStream = oFileObj.OpenTextFile(input_file, 1)

logstr = oTxtStream.ReadAll
oTxtStream.Close
Set oTxtStream = oFileObj.OpenTextFile(input_file, 1)

strarr=" "
strhigh=""
ctr=0
Do While Not oTxtStream.AtEndOfStream
    strline=oTxtStream.ReadLine
    If InStr(strarr," "& strline&" ")=0 Then 
    	strarr=strarr & strline&" "
        re.Pattern = Trim(strline)
        re.IgnoreCase = True
        re.Global = True
        Set ma = re.Execute(logstr)
        mcount=ma.Count
        'WScript.echo  strline & mCount
        If mcount > ctr Then
        	ctr=mcount
        	strhigh=strline
        ElseIf mcount = ctr Then
        	strhigh=strhigh & " and " & strline
        End if
        
    End If
    
Loop
WScript.echo "The highest (number of counts) = " & strhigh & " - Count = " &ctr

Open in new window

0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 36478278
kt2003,

Depending on what you mean by "counts per word", the code you accepted might not be delivering what you want.

For example, suppose your input file contains:

aaa
bbb
ccc
aaa
bbbbbb

The code you accepted will say that 'bbb' is the most frequently occurring word, with a count of three.  I doubt that that was what you wanted.

You also indicated in your question that you wanted to output the counts for all "words".  The code you accepted does not do that.

Patrick
0
 
LVL 12

Expert Comment

by:prashanthd
ID: 36478411
Thanks Patrick...

@kt2003 you can try the modified script.
input_file="C:\input.txt"

Set oFileObj = CreateObject("Scripting.FileSystemObject")
Set oTxtStream = oFileObj.OpenTextFile(input_file, 1)

strarr=" "
strhigh=""
ctr=0
Do While Not oTxtStream.AtEndOfStream
    strline=oTxtStream.ReadLine
    If InStr(strarr," "& strline&" ")=0 Then 
        strarr=strarr & strline&" "
        
        Set oTxtStream1 = oFileObj.OpenTextFile(input_file, 1)
        mcount=0
        Do While Not oTxtStream1.AtEndOfStream
            strline1=oTxtStream1.ReadLine
            If LCase(strline)=LCase(strline1) Then 
                mcount=mcount+1
            End If
            
        Loop
        oTxtStream1.close
        WScript.echo  strline & " - " & mCount
        If mcount > ctr Then
            ctr=mcount
            strhigh=strline
        ElseIf mcount = ctr Then
            strhigh=strhigh & " and " & strline
        End If
    End If    
Loop

WScript.echo "The highest (number of counts) = " & strhigh & " - Count = " &ctr

oTxtStream.close

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question