Feb 15th, 2006, 08:16 PM
#1
Thread Starter
Lively Member
Trying to figure this out...
I recently made a program for searching logs... Though It works, but I'm trying to make it faster.... hopefully you guys can enlighten me.
VB Code:
Private Sub btSearch_Click()
Dim Count As Integer
Dim Results As String
Dim i As Integer
Dim OneFile As String
DisableControls
LoadWordList
ProgressBar1.Value = 0
If Option1.Value = True Then
FindInText "", tbContent.Text, Count, Results
Else
ProgressBar1.Visible = True
For i = 1 To AllFiles.Count
On Error Resume Next
Open AllFiles.Item(i) For Input As #1
OneFile = Input(LOF(1), #1)
FindInText AllFiles.Item(i) & vbTab & ":", OneFile, Count, Results
Close #1
Dim NewValue As Integer
NewValue = ProgressBar1.Value + ((FileLen(AllFiles.Item(i)) / 10) / AllFilesSize) * 100
If NewValue < 100 Then
ProgressBar1.Value = ProgressBar1.Value + ((FileLen(AllFiles.Item(i)) / 10) / AllFilesSize) * 100
Else
ProgressBar1.Value = 100
End If
Next
ProgressBar1.Visible = False
End If
Form3.tbCount.Text = Count
Form3.tbResults.Text = Results
EnableControls
Form3.Show
End Sub
Private Sub FindInText(Label As String, Text As String, ByRef Count As Integer, ByRef Results As String)
Dim StartIndex, EndIndex, i As Integer
Dim Sentence As String
EndIndex = 0
While EndIndex < Len(Text)
StartIndex = EndIndex + 1
EndIndex = InStr(StartIndex, Text, vbLf, vbTextCompare)
If EndIndex = 0 Then
Sentence = Mid(Text, StartIndex)
EndIndex = Len(Text)
Else
Sentence = Mid(Text, StartIndex, EndIndex - StartIndex - 1)
End If
For i = 0 To Form2.lbWords.ListCount - 1
If InStr(1, Sentence, Form2.lbWords.List(i), vbTextCompare) > 0 Then
Results = Results + Label + Sentence + vbCrLf
Count = Count + 1
End If
Next
Wend
End Sub
Feb 15th, 2006, 08:39 PM
#2
Re: Trying to figure this out...
what are you trying to pull out from the text? is it looking for a single word and then pulling out the line that contains it?
could you provide an example of the kind of text in the log?
Feb 15th, 2006, 09:07 PM
#3
Thread Starter
Lively Member
Re: Trying to figure this out...
It's opening a directory, I have a word list it searches from as well.
Lets say I search for CVAR it would pull it looking somthing like this
L 01/15/2006 - 14:04:08: Server cvar "_tutor_bomb_viewable_check_interval" = "0.5
it shows the log it came from as well as the line its in.
Feb 15th, 2006, 10:20 PM
#4
Re: Trying to figure this out...
I'm making no claims of faster speed, this is just an alternative way of running the search.
I decided to use FileSystemObject to loop through the files just because it's quick and easy to implement for an example. Then I've dumped the contents of the log file into a richtextbox.
I noticed you were finding all the sentences and then checking for the word, it makes more sense to check for the word and then pull out the sentence. If a sentence has multiple hits then it only pulls it out once.
VB Code:
Private Sub cmdSearch_Click()
Dim fso As New FileSystemObject
Dim oFolder As Folder
Dim oFile As File
Set oFolder = fso.GetFolder(Text1.Text)
rtbResults.Text = vbNullString
rtbResults.Locked = True
For Each oFile In oFolder.Files
If UCase$(Right$(oFile.Name, 3)) = "LOG" Then
rtbBuffer.LoadFile oFile.Path, rtfText
FindText oFile
End If
Next oFile
If rtbResults.Text = vbNullString Then rtbResults.Text = "None Found"
rtbResults.Locked = False
End Sub
Private Sub FindText(ByVal fFile As File)
Dim lStart As Long
Dim lPos As Long
Dim lSentenceStart As Long
Dim lSentenceFinish As Long
Dim N As Integer
For N = 0 To cboSearch.ListCount - 1
lPos = InStr(1, rtbBuffer.Text, cboSearch.List(N), vbTextCompare)
If lStart = 0 Then
lStart = lPos
ElseIf lStart > lPos And Not lPos = 0 Then
lStart = lPos
End If
Next N
Do Until lStart = 0
lSentenceStart = InStrRev(rtbBuffer.Text, vbLf, lStart)
lSentenceFinish = InStr(lStart, rtbBuffer.Text, vbLf)
If lSentenceFinish = 0 Then lSentenceFinish = Len(rtbBuffer.Text)
rtbResults.Text = rtbResults.Text & fFile.Name & " - " & Mid$(rtbBuffer.Text, lSentenceStart + 1, lSentenceFinish - lSentenceStart)
If lSentenceFinish = Len(rtbBuffer.Text) Then
rtbResults.Text = rtbResults.Text & vbCrLf
Exit Do
End If
lStart = 0
For N = 0 To cboSearch.ListCount - 1
lPos = InStr(lSentenceFinish, rtbBuffer.Text, cboSearch.List(N), vbTextCompare)
If lStart = 0 Then
lStart = lPos
ElseIf lStart > lPos And Not lPos = 0 Then
lStart = lPos
End If
Next N
Loop
End Sub
Private Sub Form_Load()
Text1.Text = App.Path
With cboSearch
.AddItem "CVAR"
.AddItem "tutor"
.AddItem "bushmobile"
.AddItem "banana"
.AddItem "shoe"
.ListIndex = 0
End With
End Sub
I have attached the project as well so you don't have to bother fixing to your purposes to test it. Just download it, unzip it, copy all your log files into the same folder, check that it's checking for the right extension and then run it.
I have no idea if it'll be faster or not.
I've noticed it can run slowly on the very first run after loading up the project (so don't judge it on that)
Attached Files
Last edited by bushmobile; Feb 15th, 2006 at 10:50 PM .
Feb 15th, 2006, 10:22 PM
#5
Re: Trying to figure this out...
i've just noticed yours is checking for lots of words at once, i'll try and add that in.
[edit] I've made those changes and updated the code above & the download [/edit]
Last edited by bushmobile; Feb 15th, 2006 at 10:47 PM .
Feb 15th, 2006, 11:22 PM
#6
Re: Trying to figure this out...
I've just done a few dirty speed tests on the actual FindText subs. I modified yours so it would work with my file loading.
Searching about 14000 lines, for an uncommon word (once every 60 lines), mine took 1.6 secs, yours 4.3.
However, searching about 7000 lines for very common words (occuring in 3 of every 4 lines) it was about 42 to 44.
And with 1500 lines with 8 occurences of the same word they came out almost dead even at 25 secs.
So it seems that depends on the frequency of the words your searching for. As for whether loading it into a RTB is faster I have no idea.
Anyway, that's my 2 cents. I'm off to bed.
Feb 16th, 2006, 07:27 AM
#7
Re: Trying to figure this out...
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width