Project Gutenberg has some books in text file format. My code seems woefully slow compared to BC_Programmer's. Although either script counted "God" in the King James Bible in less than half a second. but see below...
Salmon-count.vbs
This is how I am going to try to do VBscripts in future...
Option Explicit
'Setup
Dim ObjFSO
Dim ObjTS
Dim StrFileName
Dim StrLookString
Dim StrThisline
Dim SngStartSec
Dim SngEndSec
Dim SngElapsed
Dim SngLineCount
Dim SngTotalCount
Dim SngSubsLen
Dim SngSubStart
Dim SngCaseSensitive
'Input filename
StrFileName=Wscript.Arguments(0)
'String to search for
StrLookString=Wscript.Arguments(1)
'Case type - 1 = case sensitive 0 = case insensitive
SngCaseSensitive = Wscript.Arguments (2)
'Length of string to search for
SngSubsLen = Len (StrLookString)
'if case insensitive search
'convert to lower case
If SngCaseSensitive = 0 Then StrLookString = LCase(StrLookString)
'Initialise File System Object
Set ObjFSO=Createobject("Scripting.Filesystemobject")
'Open input file
Set ObjTS=ObjFSO.Opentextfile(StrFileName)
'Store start time (secs since midnight)
SngStartSec = Timer
'Keep reading lines until all done
Do While Not ObjTS.Atendofstream
'Get line
StrThisLine=ObjTS.Readline
'if case insensitive search
'convert to lower case
If SngCaseSensitive = 0 Then StrThisLine=LCase(StrThisLine)
'Set count to zero
SngLineCount = 0
Do
'Is string in line? If so, get place
SngSubStart = InStr ( StrThisLine, StrLookString )
'If found, add 1 to counter
If SngSubStart > 0 then SngLineCount = SngLineCount + 1
'If found, chop off string before
StrThisLine = Mid ( StrThisLine, ( SngSubstart + SngSubsLen ) )
'Exit when no more found
Loop Until SngSubstart = 0
'Add count from this line to total
SngTotalCount = SngTotalCount + SngLineCount
Loop
'Close input file
ObjTS.Close
'Store end time (secs since midnight)
SngEndSec = Timer
'Subtract to get elapsed
SngElapsed = SngEndsec - SngStartSec
'Show results
wscript.echo SngTotalCount
wscript.echo formatnumber(SngElapsed,3)
BCP_count.vbs
Function GetCountStr(ByVal searchIn, ByVal SearchFor,Byval CompareText)
CompareText=CBool(CompareText)
GetCountStr = (Len(searchIn) - Len(Replace(searchIn, SearchFor, "",1,-1,abs(CompareText)))) / Len(SearchFor)
End Function
dim inputstrm
Dim lookin,lookfor
Dim StartSec, Endsec, Elapsed
'see if /i was specified....
for each looparg in WScript.Arguments
If UCase(looparg)="-I" or UCase(looparg)="/I" Then
ignorecase=true
Exit For
End If
Next
Startsec=Timer
Set inputstrm = CreateObject("Scripting.FileSystemObject").OpenTextFile(WScript.Arguments(0))
lookfor = WScript.Arguments(1)
lookin=inputstrm.ReadAll()
Endsec=Timer
Elapsed=Endsec - Startsec
WScript.Echo GetCountStr(lookin,lookfor,ignorecase)
wscript.echo Formatnumber(Elapsed, 3)
Salmon-count.vbs "H G Wells The War Of The Worlds.txt" "Martians" 1
156
0.043
BCP-count.vbs "H G Wells The War Of The Worlds.txt" "Martians"
156
0.016
Salmon-count.vbs "Complete Works Of Shakespeare.txt" "Hamlet" 1
113
0.688
BCP-count.vbs "Complete Works Of Shakespeare.txt" "Hamlet"
113
0.250
Salmon-count.vbs "Tolstoy War And Peace.txt" "Pierre" 1
1963
0.383
BCP-count.vbs "Tolstoy War And Peace.txt" "Pierre"
1963
0.145
Salmon-count.vbs "King James Bible.txt" "God" 1
4167
0.359
BCP-count.vbs "King James Bible.txt" "God"
4167
0.188
Salmon-count.vbs "Samuel Richardson Clarissa.txt" "she" 1
8861
1.156
bcp-count.vbs "Samuel Richardson Clarissa.txt" "she"
8861
0.234
but...
I downloaded a text file containing 1 million places of pi (1,000,000,002 bytes) with no carriage returns. I figured that my code wouldn't like that, so I used GNU fold to insert cr/lf pairs every 80 columns. However, when I tried BCP's code on it, oh dear! The system got awfully sluggish and I watched my available RAM go down from 3.2 GB to 24 MB before I used Process Explorer to terminate cscript.exe. But my "slow" code just chewed its way through in 1 minute 44 and a bit seconds...
salmon-count.vbs "1 billion places of pi.txt" "567" 0
975498
104.430
351,218 H G Wells The War Of The Worlds.txt
3,288,738 Tolstoy War And Peace.txt
4,397,206 King James Bible.txt
5,582,655 Complete Works Of Shakespeare.txt
5,616,676 Samuel Richardson Clarissa.txt
1,025,000,002 1 billion places of pi.txt
System:
Shuttle SN78SH7, AMD Phenom II 945 (quad core), 4 GB Crucial 800 MHz DDR2 RAM, Windows 7 64 bit, files read from Seagate 320GB external USB 2.0 drive.