Results 1 to 9 of 9

Thread: File Content Search

  1. #1

    Thread Starter
    Lively Member amolt's Avatar
    Join Date
    Aug 2006
    Location
    INDIA
    Posts
    80

    Question File Content Search

    Hi all,

    expecting some ideas to create an application which search for the contents in a file.
    Microsoft Windows Search 4.0 or the Search included in Windows 7 does the same.
    I have already implemented the Windows Search so I am not looking for the Windows Search implementation.
    Any way to create a program using VB6.0 ?

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,624

    Re: File Content Search

    Once you open your file, use instr() function

  3. #3
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,624

    Re: File Content Search

    Here's a quick example to find the FIRST occurrence of a string within a text file. You can modify to find more occurrences easily.

    Code:
    Private Sub Command6_Click()
    Dim mySearchText As String, x As Integer
     Dim fnum As Integer, myFileContents As String
     mySearchText = "Fred"
        fnum = FreeFile
        Open App.Path & "\test.txt" For Input As fnum
        myFileContents = Input(LOF(fnum), fnum)
        Close fnum
        x = InStr(1, myFileContents, mySearchText)
        If x > 0 Then
            MsgBox Mid(myFileContents, x, Len(mySearchText))
        End If
    End Sub

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: File Content Search

    If what you are asking is how to search through a lot of files and folders of files then you should be aware that using something like Instr() on each file will be extremely slow compared to windows search which uses indexing.

  5. #5
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,667

    Re: File Content Search

    I'd recommend the file search class from this:

    http://leandroascierto.com/blog/clase-para-buscar/

    The function it uses to search files seems pretty quick; it uses API and byte arrays... much faster than user regular vb instr code. And it will work with large files.

    If you can get past the language barrier, there's some truly amazing code on that site. vbAccelerator level stuff using simple class modules instead of nightmarish user control active x crap.
    Code:
    Private Function FindWordInFile(ByVal sPath As String, ByVal sWord As String, Optional ByVal bUnicode As Boolean) As Boolean
        Dim bArray() As Byte
        Dim lRet As Long
        Dim hFile As Long
        Dim sFind() As Byte
        Dim s As String
        Dim t As Long
        Dim i As Long
        Dim FileSize As Currency
        Dim tLI As LARGE_INTEGER
        Dim LenBuffer As Long
        Dim CurPos As Currency
    
        sWord = UCase(sWord)
        If bUnicode Then sWord = StrConv(sWord, vbUnicode)
        sFind = StrConv(sWord, vbFromUnicode)
    
        hFile = CreateFile(sPath, GENERIC_READ, FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING, 0, 0)
        
        If hFile <> INVALID_HANDLE_VALUE Then
        
            
            tLI.LowPart = GetFileSize(hFile, tLI.HighPart)
        
            LenBuffer = &H2800 ' 10 KB
        
            FileSize = LargeIntToCurrency(tLI.LowPart, tLI.HighPart)
            
            If FileSize < UBound(sFind) Then GoTo OutSearch
        
            If LenBuffer > FileSize Then LenBuffer = FileSize
        
            ReDim bArray(LenBuffer - 1)
     
            Do
                ReadFile hFile, bArray(0), UBound(bArray) + 1, lRet, 0&
                
                If lRet = 0 Then Exit Do
                
                CurPos = CurPos + lRet
    
                If lRet < LenBuffer Then
                    ReDim Preserve bArray(lRet)
                End If
    
                If InBytes(bArray, sFind) <> -1 Then
                    FindWordInFile = True
                    Exit Do
                End If
                
                If CurPos = FileSize Then Exit Do
                
                tLI = CurrencyToLargeInt(CurPos - UBound(sFind) + 1)
                
                SetFilePointer hFile, tLI.LowPart, tLI.HighPart, FILE_BEGIN
                            
                If c_bCancel Then GoTo OutSearch
            Loop
            
    OutSearch:
            
            CloseHandle hFile
    
        End If
    End Function
    
    
    Private Function InBytes(ByRef bvSource() As Byte, ByRef bvMatch() As Byte) As Long
    
        Dim i       As Long
        Dim j       As Long
        Dim lChr    As Byte
        Dim LenMach As Long
     
        LenMach = UBound(bvMatch)
        
        lChr = bvMatch(0)
        
        If LenMach > 0 Then
        
            For i = 0 To UBound(bvSource) - LenMach
          
                If (lChr = aUChars(bvSource(i))) Then
    
                    j = LenMach - 1
        
                    Do
                        If bvMatch(j) <> aUChars(bvSource(i + j)) Then GoTo NotEqual
                        j = j - 1
                    Loop While j
                    
                    InBytes = i
                    
                    Exit Function
        
                End If
    NotEqual:
            
            Next
        
        Else
            For i = 0 To UBound(bvSource)
                If (lChr = aUChars(bvSource(i))) Then
                    InBytes = i
                    Exit Function
                End If
            Next
        End If
    
        InBytes = -1
    End Function

  6. #6

    Thread Starter
    Lively Member amolt's Avatar
    Join Date
    Aug 2006
    Location
    INDIA
    Posts
    80

    Re: File Content Search

    Hi to all,
    Thanks for the replies.

    Sam, yes it is easy to look with InStr within a single file or few bunch of files as well if the file size also comparatively less.
    As DataMiser got the point the InStr will be extremely slow in the case of lot of files in a folder or folders.

    As Windows Search uses indexing of files and look for a string in to it.
    The basic problem of using Windows Search as facing is it can not index the mapped drives path or a URL path.
    As in the case of a reporting application where we need to provide a string search in report contents and display the resultant files.
    Here if the report creation path is not local or placed on NAS or any other shared folder on other server, then it is quite impossible to use Windows Search for such locations.
    So rather than implementing the Windows Search I am thinking to develop some application which follows some Windows Search techniques like creating Indexes or may be anyother way where we can search on any given path.

  7. #7

    Thread Starter
    Lively Member amolt's Avatar
    Join Date
    Aug 2006
    Location
    INDIA
    Posts
    80

    Re: File Content Search

    #fafalone a great link you given. Thanks to you.
    Will trying to work around with it.

    Any ideas, for creating the index of files.

  8. #8
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,667

    Re: File Content Search

    I didn't think Windows Search indexed the content of the files? Not sure how you would do that without just copying it.

  9. #9

    Thread Starter
    Lively Member amolt's Avatar
    Join Date
    Aug 2006
    Location
    INDIA
    Posts
    80

    Re: File Content Search

    Windows Search indexed locations are configured from control panel item "IndexingOptions" if you are using Windows 7 u can see it there. For WinXP systems we need to install Windows Search 4.0

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