How to Count Keywords in Several Files
Keywords: Count Keywords in Several Files
Question:
I am having trouble finding away to get some information. I have log files that I have to keep for a week.
Each week the log files are deleted and I get new ones. The log file contains names, such as Bob, Frank,
Sam etc..
Each log file might have those names or different ones. So it would look like this, the files would be
L0925000.log and L0925001.log and so on. The log file looks like this:
Sam
Bob
Frank
etc...
The next log file might have the names or different name such as Ryan, Brooke, Tammy etc... I need a
count of how many times each name might show up in all the log files. So I would have 20 log files and Bob
would show up 10 times
(Bob = 10) (Tammy = 5) etc...
How would I do this? Thanks for all the great help!!
Answer:
If you knew in advance what the names were, you could do something like:
Bob=0
Frank=0
Ted=0
Tammy=0
DirChange("C:\TEMP\Names")
allfiles=FileItemize("*.txt")
count=ItemCount(allfiles,@tab)
for i=1 to count
eachfile=ItemExtract(i,allfiles,@tab)
; Find number of Bob, Frank, Ted, Tammy in eachfile
fs1 = FileSize(eachfile)
binbuf1 = binaryalloc( fs1 )
BinaryRead( binbuf1, eachfile)
Bob= Bob+BinaryStrCnt( binbuf1, 0, fs1 - 1, "Bob")
Frank= Frank+BinaryStrCnt( binbuf1, 0, fs1 - 1, "Frank")
Ted= Ted+BinaryStrCnt( binbuf1, 0, fs1 - 1, "Ted")
Tammy= Tammy+BinaryStrCnt( binbuf1, 0, fs1 - 1, "Tammy")
BinaryFree( binbuf1 )
next
Message( "Totals are:", "Bob count=%Bob%%@crlf%Frank count=%Frank%%@crlf%Ted count=%Ted%%@crlf%Tammy count=%Tammy%" )
Otherwise, if there are multiple unknown names, you could do something like:
DirChange("C:\incoming")
list=Fileitemize("*.log")
count=ItemCount(list,@tab)
datafile="c:\incoming\datafile.ini"
IniDeletePvt("UserCounts",@WHOLESECTION,datafile)
for xx=1 to count
thisfile=ItemExtract(xx,list,@tab)
handle=FileOpen(thisfile,"READ")
while 1
line=FileRead(handle)
if line == "*EOF*" then break
line=Strlower(line) ; run in lowercase
usercount=IniReadPvt("UserCounts",line,0,datafile)
IniWritePvt("UserCounts",line,usercount+1,datafile)
endwhile
FileClose(handle)
next
IniWritePvt("","","",datafile)
Message("All Doned","See datafile.ini")
Article ID: W14590
Filename: Count Keywords in Several Files.txt