Results 1 to 7 of 7

Thread: Trying to figure this out...

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    77

    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:
    1. Private Sub btSearch_Click()
    2. Dim Count As Integer
    3. Dim Results As String
    4. Dim i As Integer
    5. Dim OneFile As String
    6.  
    7. DisableControls
    8. LoadWordList
    9. ProgressBar1.Value = 0
    10.  
    11. If Option1.Value = True Then
    12.     FindInText "", tbContent.Text, Count, Results
    13. Else
    14.     ProgressBar1.Visible = True
    15.     For i = 1 To AllFiles.Count
    16.         On Error Resume Next
    17.         Open AllFiles.Item(i) For Input As #1
    18.         OneFile = Input(LOF(1), #1)
    19.         FindInText AllFiles.Item(i) & vbTab & ":", OneFile, Count, Results
    20.         Close #1
    21.         Dim NewValue As Integer
    22.         NewValue = ProgressBar1.Value + ((FileLen(AllFiles.Item(i)) / 10) / AllFilesSize) * 100
    23.         If NewValue < 100 Then
    24.             ProgressBar1.Value = ProgressBar1.Value + ((FileLen(AllFiles.Item(i)) / 10) / AllFilesSize) * 100
    25.         Else
    26.             ProgressBar1.Value = 100
    27.         End If
    28.     Next
    29.     ProgressBar1.Visible = False
    30. End If
    31.  
    32. Form3.tbCount.Text = Count
    33. Form3.tbResults.Text = Results
    34.  
    35. EnableControls
    36.  
    37. Form3.Show
    38. End Sub
    39.  
    40. Private Sub FindInText(Label As String, Text As String, ByRef Count As Integer, ByRef Results As String)
    41. Dim StartIndex, EndIndex, i As Integer
    42. Dim Sentence As String
    43.  
    44. EndIndex = 0
    45.  
    46. While EndIndex < Len(Text)
    47.     StartIndex = EndIndex + 1
    48.     EndIndex = InStr(StartIndex, Text, vbLf, vbTextCompare)
    49.     If EndIndex = 0 Then
    50.         Sentence = Mid(Text, StartIndex)
    51.         EndIndex = Len(Text)
    52.     Else
    53.         Sentence = Mid(Text, StartIndex, EndIndex - StartIndex - 1)
    54.     End If
    55.    
    56.     For i = 0 To Form2.lbWords.ListCount - 1
    57.         If InStr(1, Sentence, Form2.lbWords.List(i), vbTextCompare) > 0 Then
    58.             Results = Results + Label + Sentence + vbCrLf
    59.             Count = Count + 1
    60.         End If
    61.     Next
    62. Wend
    63.  
    64. End Sub

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2006
    Posts
    77

    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.

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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:
    1. Private Sub cmdSearch_Click()
    2.   Dim fso As New FileSystemObject
    3.   Dim oFolder As Folder
    4.   Dim oFile As File
    5.  
    6.   Set oFolder = fso.GetFolder(Text1.Text)
    7.  
    8.   rtbResults.Text = vbNullString
    9.   rtbResults.Locked = True
    10.  
    11.   For Each oFile In oFolder.Files
    12.     If UCase$(Right$(oFile.Name, 3)) = "LOG" Then
    13.       rtbBuffer.LoadFile oFile.Path, rtfText
    14.       FindText oFile
    15.     End If
    16.   Next oFile
    17.  
    18.   If rtbResults.Text = vbNullString Then rtbResults.Text = "None Found"
    19.   rtbResults.Locked = False
    20. End Sub
    21.  
    22. Private Sub FindText(ByVal fFile As File)
    23.   Dim lStart As Long
    24.   Dim lPos As Long
    25.   Dim lSentenceStart As Long
    26.   Dim lSentenceFinish As Long
    27.   Dim N As Integer
    28.  
    29.   For N = 0 To cboSearch.ListCount - 1
    30.     lPos = InStr(1, rtbBuffer.Text, cboSearch.List(N), vbTextCompare)
    31.     If lStart = 0 Then
    32.       lStart = lPos
    33.     ElseIf lStart > lPos And Not lPos = 0 Then
    34.       lStart = lPos
    35.     End If
    36.   Next N
    37.  
    38.   Do Until lStart = 0
    39.     lSentenceStart = InStrRev(rtbBuffer.Text, vbLf, lStart)
    40.     lSentenceFinish = InStr(lStart, rtbBuffer.Text, vbLf)
    41.     If lSentenceFinish = 0 Then lSentenceFinish = Len(rtbBuffer.Text)
    42.     rtbResults.Text = rtbResults.Text & fFile.Name & " - " & Mid$(rtbBuffer.Text, lSentenceStart + 1, lSentenceFinish - lSentenceStart)
    43.     If lSentenceFinish = Len(rtbBuffer.Text) Then
    44.       rtbResults.Text = rtbResults.Text & vbCrLf
    45.       Exit Do
    46.     End If
    47.     lStart = 0
    48.     For N = 0 To cboSearch.ListCount - 1
    49.       lPos = InStr(lSentenceFinish, rtbBuffer.Text, cboSearch.List(N), vbTextCompare)
    50.       If lStart = 0 Then
    51.         lStart = lPos
    52.       ElseIf lStart > lPos And Not lPos = 0 Then
    53.         lStart = lPos
    54.       End If
    55.     Next N
    56.   Loop
    57. End Sub
    58.  
    59. Private Sub Form_Load()
    60.   Text1.Text = App.Path
    61.   With cboSearch
    62.     .AddItem "CVAR"
    63.     .AddItem "tutor"
    64.     .AddItem "bushmobile"
    65.     .AddItem "banana"
    66.     .AddItem "shoe"
    67.     .ListIndex = 0
    68.   End With
    69. 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 Attached Files
    Last edited by bushmobile; Feb 15th, 2006 at 10:50 PM.

  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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.

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    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.

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Trying to figure this out...

    Moved to Code It Better

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width