Solved

Help with vbs

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

758 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

22 Experts available now in Live!

Get 1:1 Help Now