How can i count words in vbs script?
I have a script but this script count the readline but not the word
"Failed".
Can anybody help me?

Here is the scipt
Const FOR_READING = 1

Const FOR_WRITING = 2

Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")

Dim sLogFile : sLogFile = "replicatie1.txt"

Dim oLogFile : Set oLogFile = oFSO.OpenTextFile (sLogFile,FOR_WRITING,true)

filePath= "replicatie.txt"

srchFor= "Failed"

Set fs = CreateObject("Scripting.FileSystemObject")

Set fl = fs.OpenTextFile(filePath,1)

Dim rslt1


intCount = 0


Do Until fl.AtEndOfStream

cLine = fl.ReadLine

If InStr(LCase(cLine),srchFor) Then rslt1=rslt1 & Left(cLine,80) &
vbNewLine


Loop

intCount = intCount + 1

wscript.Echo rslt1 & " " & CStr(intcount)

Re: Count words in textfiles by Tom

Tom
Wed Nov 28 11:30:46 PST 2007

On Nov 28, 2:15 pm, "rva" <nos...@hotmail.com> wrote:
> How can i count words in vbs script?
> I have a script but this script count the readline but not the word
> "Failed".
> Can anybody help me?
>
> Here is the scipt
> Const FOR_READING = 1
>
> Const FOR_WRITING = 2
>
> Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
>
> Dim sLogFile : sLogFile = "replicatie1.txt"
>
> Dim oLogFile : Set oLogFile = oFSO.OpenTextFile (sLogFile,FOR_WRITING,true)
>
> filePath= "replicatie.txt"
>
> srchFor= "Failed"
>
> Set fs = CreateObject("Scripting.FileSystemObject")
>
> Set fl = fs.OpenTextFile(filePath,1)
>
> Dim rslt1
>
> intCount = 0
>
> Do Until fl.AtEndOfStream
>
> cLine = fl.ReadLine
>
> If InStr(LCase(cLine),srchFor) Then rslt1=rslt1 & Left(cLine,80) &
> vbNewLine
>
> Loop
>
> intCount = intCount + 1
>
> wscript.Echo rslt1 & " " & CStr(intcount)

The form of the InStr() function you are using is case sensitive ...

If InStr(LCase(cLine),srchFor) ...

and your search string has an uppercase letter, while you have
converted your input string to all lower case. Either convert BOTH to
LCase or use a case insensitive form of the InStr() function, like
this ...

If InStr(1,cLine,srchFor,vbTextCompare) ...

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/

Re: Count words in textfiles by Pegasus

Pegasus
Wed Nov 28 11:27:54 PST 2007


"rva" <nospam@hotmail.com> wrote in message
news:474dbd55$0$96606$dbd4d001@news.wanadoo.nl...
> How can i count words in vbs script?
> I have a script but this script count the readline but not the word
> "Failed".
> Can anybody help me?
>
> Here is the scipt
> Const FOR_READING = 1
>
> Const FOR_WRITING = 2
>
> Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
>
> Dim sLogFile : sLogFile = "replicatie1.txt"
>
> Dim oLogFile : Set oLogFile = oFSO.OpenTextFile
> (sLogFile,FOR_WRITING,true)
>
> filePath= "replicatie.txt"
>
> srchFor= "Failed"
>
> Set fs = CreateObject("Scripting.FileSystemObject")
>
> Set fl = fs.OpenTextFile(filePath,1)
>
> Dim rslt1
>
>
> intCount = 0
>
>
> Do Until fl.AtEndOfStream
>
> cLine = fl.ReadLine
>
> If InStr(LCase(cLine),srchFor) Then rslt1=rslt1 & Left(cLine,80) &
> vbNewLine
>
>
> Loop
>
> intCount = intCount + 1
>
> wscript.Echo rslt1 & " " & CStr(intcount)

You're searching for the word "Failed" in a test line that contains
only lower case letters!



Re: Count words in textfiles by ekkehard

ekkehard
Wed Nov 28 11:54:07 PST 2007

rva schrieb:
> How can i count words in vbs script?
> I have a script but this script count the readline but not the word
> "Failed".
> Can anybody help me?
>
> Here is the scipt
> Const FOR_READING = 1
>
> Const FOR_WRITING = 2
>
> Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
>
> Dim sLogFile : sLogFile = "replicatie1.txt"
>
> Dim oLogFile : Set oLogFile = oFSO.OpenTextFile (sLogFile,FOR_WRITING,true)
>
> filePath= "replicatie.txt"
>
> srchFor= "Failed"
>
> Set fs = CreateObject("Scripting.FileSystemObject")
>
> Set fl = fs.OpenTextFile(filePath,1)
>
> Dim rslt1
>
>
> intCount = 0
>
>
> Do Until fl.AtEndOfStream
>
> cLine = fl.ReadLine
>
> If InStr(LCase(cLine),srchFor) Then rslt1=rslt1 & Left(cLine,80) &
> vbNewLine
>
>
> Loop
>
> intCount = intCount + 1
>
> wscript.Echo rslt1 & " " & CStr(intcount)
>
>
You increment intCount *after* the loop. If you are interested in
different methods to solve your problem, you may try this:

Dim oFS : Set oFS = CreateObject( "Scripting.FileSystemObject" )
Dim sFSpec : sFSpec = ".\access.log"
Dim oFile : Set oFile = oFS.GetFile( sFSpec )
Dim sWord : sWord = "192.168.101.24" ' "GET"
Dim dtStart

WScript.Echo "Searching for", sWord, "in", sFSpec, "Size:", oFile.Size, "using RegExp"
dtStart = Now
Dim oRE : Set oRE = New RegExp
Dim oMTS
oRE.Pattern = sWord
oRE.Global = True
oRE.IgnoreCase = True
Set oMTS = oRE.Execute( oFile.OpenAsTextStream( ForReading ).ReadAll )
WScript.Echo "found it", oMTS.Count, "times in ", DateDiff( "s", dtStart, Now ), "secs"

WScript.Echo "Searching for", sWord, "in", sFSpec, "Size:", oFile.Size, "using
ReadLine/vbTextCompare"
dtStart = Now
Dim oTS : Set oTS = oFile.OpenAsTextStream( ForReading )
Dim nLines : nLines = 0
Dim nOcc : nOcc = 0
Do Until oTS.AtEndOfStream
nLines = nLines + 1
If 0 < InStr( 1, oTS.ReadLine, sWord, vbTextCompare ) Then
nOcc = nOcc + 1
End If
Loop
oTS.Close

WScript.Echo "found it", nOcc, "times in", nLines, "lines in ", DateDiff( "s", dtStart,
Now ), "secs"
WScript.Echo "Searching for", sWord, "in", sFSpec, "Size:", oFile.Size, "using
ReadLine/LCase"
dtStart = Now
Set oTS = oFile.OpenAsTextStream( ForReading )
nLines = 0
nOcc = 0
Do Until oTS.AtEndOfStream
nLines = nLines + 1
If InStr( LCase( oTS.ReadLine ), sWord ) Then
nOcc = nOcc + 1
End If
Loop
WScript.Echo "found it", nOcc, "times in", nLines, "lines in ", DateDiff( "s", dtStart,
Now ), "secs"

Output:
=== countWord: count word in file =============================================
Searching for 192.168.101.24 in .\access.log Size: 35164544 using RegExp
found it 3889 times in 3 secs
Searching for 192.168.101.24 in .\access.log Size: 35164544 using ReadLine/vbTextCompare
found it 3889 times in 360004 lines in 8 secs
Searching for 192.168.101.24 in .\access.log Size: 35164544 using ReadLine/LCase
found it 3889 times in 360004 lines in 6 secs
=== countWord: 0 done (00:00:17) ==============================================




Re: Count words in textfiles by rva

rva
Thu Nov 29 11:03:49 PST 2007

Thank you , I forgot the command Lcase.


"rva" <nospam@hotmail.com> schreef in bericht
news:474dbd55$0$96606$dbd4d001@news.wanadoo.nl...
> How can i count words in vbs script?
> I have a script but this script count the readline but not the word
> "Failed".
> Can anybody help me?
>
> Here is the scipt
> Const FOR_READING = 1
>
> Const FOR_WRITING = 2
>
> Dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
>
> Dim sLogFile : sLogFile = "replicatie1.txt"
>
> Dim oLogFile : Set oLogFile = oFSO.OpenTextFile
> (sLogFile,FOR_WRITING,true)
>
> filePath= "replicatie.txt"
>
> srchFor= "Failed"
>
> Set fs = CreateObject("Scripting.FileSystemObject")
>
> Set fl = fs.OpenTextFile(filePath,1)
>
> Dim rslt1
>
>
> intCount = 0
>
>
> Do Until fl.AtEndOfStream
>
> cLine = fl.ReadLine
>
> If InStr(LCase(cLine),srchFor) Then rslt1=rslt1 & Left(cLine,80) &
> vbNewLine
>
>
> Loop
>
> intCount = intCount + 1
>
> wscript.Echo rslt1 & " " & CStr(intcount)
>
>