0

how can i Read a txt file and output all words wtih a number of how many words there are.

example

hey my name is is hey hey hey i like like this

hey : 4
my : 1
name: 1
is: 2
i : 1
like 2
this : 1

i have this:

'Option Explicit


Dim objTextFile,ForReading,objFSO,objOpen







Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOpen = objFSO.OpenTextFile("C:\Users\mib\.oracle_jre_usage\Desktop\olsenbanden.txt")
strContents = CStr(objOpen.ReadAll)

WScript.Echo(strContents)

okay i have tried this now but doesn't work:

Dim x
x=10

set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Users\mib\.oracle_jre_usage\Desktop\olsenbanden.txt",1)
Dim strLine
Dim total_words
total_words = 0
do while not objFileToRead.AtEndOfStream
     strLine = objFileToRead.ReadLine()

     a=Split(strLine,":")
     for each x in a
        if IsNumeric(x) then
            total_words = total_words + 1
        end if
     next
loop

objFileToRead.Close
Set objFileToRead = Nothing

WScript.Echo(IsNumeric(x) & "<br />")
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Mikkel
  • 29
  • 6

2 Answers2

1

I think you want to count each word in the file? You could use a dictionary to collect the words, then write that out at the end after you've processed the document

Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(your_file,1)

' dictionary object for collecting and counting the words
dim dictionary: set dictionary = CreateObject("Scripting.Dictionary")
dictionary.CompareMode = 1 'TextCompare

do while not objFileToRead.AtEndOfStream
     strLine = objFileToRead.ReadLine()

     a=Split(strLine," ")
     for each x in a
        ' add new word or increment existing word
        ' you might want to do some sanity checks here
        if not dictionary.exists(x) then
             dictionary.add x, 1
        else
             dictionary(x) = dictionary(x) + 1
        end if
     next
loop

' output the result
dim k
for each k in dictionary.Keys()
    WScript.Echo(k & ":" & dictionary(k) & vbNewLine)
next 'k

Not sure if you're using Windows Script Host or ASP, but all you need to do is change the Wscript.Echo statements to the equivalent ASP etc

  • The nested condition is not required. `dictionary(x) + 1` will evaluate to 1 in case of an empty (non-existing) entry, so you could simply do `dictionary(x) = dictionary(x) + 1` inside the `for each` loop. – Ansgar Wiechers Jan 11 '16 at 09:12
0

you could use IsNumeric(value) function.

Example:

Dim x
x=10
response.write(IsNumeric(x) & "<br />")

Your code could be like this:

Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(your_file,1)
Dim strLine
Dim total_words
total_words = 0
do while not objFileToRead.AtEndOfStream
     strLine = objFileToRead.ReadLine()

     a=Split(strLine,":")
     for each x in a
        if IsNumeric(x) then
            total_words = total_words + 1
        end if
     next
loop

objFileToRead.Close
Set objFileToRead = Nothing

Atention: I have not tried it so I could have an error...

Good luck!!

raBinn
  • 159
  • 1
  • 11