CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2001
    Location
    CO
    Posts
    4

    Finding text in a large file

    Is there a way to find text within a large file (several megabytes)? I'm trying to find out if a chm file contains a topic, and it looks like the topics are listed at the top. Alternatively, if there is something that allows me to find this out, rather than open a dialog with "page not found" displayed, that would work to solve my problem.
    Thanks in advance.



  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: Finding text in a large file

    Look at this code. It might help you

    Find and replace a string in all files matching a pattern
    in a directory

    Use Dir$ to find the files. For each file, read the file and use Replace
    to make the replacement.

    Note that the find or replace text can contain carriage returns.


    Private Sub cmdReplace_Click()
    Dim from_text As String
    Dim to_text As String
    Dim dir_name As String
    Dim patterns As Variant
    Dim file_name As String
    Dim i As Integer
    Dim results As String

    ' Get the text to find and replace.
    from_text = txtFind.Text
    to_text = txtReplace.Text

    ' Get the directory name.
    dir_name = FileList.Path
    If Right$(dir_name, 1) <> "\" Then dir_name = dir_name & "\"

    ' Get the file patterns.
    patterns = Split(FileList.Pattern, ";")

    results = "Files:"

    ' Repeat for each pattern.
    For i = LBound(patterns) To UBound(patterns)
    ' Add the pattern to the file name.
    file_name = Dir$(dir_name & patterns(i))
    Do While Len(file_name) > 0
    ' Process this file.
    results = results & " " & file_name
    ReplaceInFile dir_name & file_name, from_text, to_text

    ' Get the next file.
    file_name = Dir$()
    Loop
    Next i

    MsgBox results
    End Sub

    ' In the file file_name, replace occurrances of from_text
    ' with to_text.
    Private Sub ReplaceInFile(ByVal file_name As String, ByVal from_text
    As String, ByVal to_text As String)
    Dim fnum As Integer
    Dim file_text As String

    ' Read the file.
    fnum = FreeFile
    Open file_name For Input As fnum
    file_text = Input$(LOF(fnum), #fnum)
    Close #fnum

    ' Replace the text.
    file_text = Replace(file_text, from_text, to_text)

    ' Rewrite the file.
    fnum = FreeFile
    Open file_name For Output As fnum
    Print #fnum, file_text
    Close #fnum
    End Sub






    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    Oct 2001
    Location
    CO
    Posts
    4

    Re: Finding text in a large file

    Thanks for the quick response.

    The file I am looking at is 3Mb. Wouldn't reading it into a string be bad news?



  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Finding text in a large file

    Here how to quick retrieve the content of a file in binary mode

    http://codeguru.com/cgi-bin/bbs/wt/s...age=&view=&sb=

    You can then search in the result array for what you need.
    This should be quicker in opening and retrieving data.


    Special thanks to Lothar "the Great" Haensler, Tom Archer, Chris Eastwood, TCartwright, Bruno Paris, Dr_Michael
    and all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

    The Rater
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured