Solved

Help with vbs

Posted on 2011-09-03
3
358 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

821 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