Solved

Help with vbs

Posted on 2011-09-03
3
349 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
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
prashanthd earned 500 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 92

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now