Click to See Complete Forum and Search --> : Finding text in a large file


altinbay
October 10th, 2001, 10:30 AM
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.

Iouri
October 10th, 2001, 10:58 AM
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
iouri@hotsheet.com

altinbay
October 10th, 2001, 04:33 PM
Thanks for the quick response.

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

Cimperiali
October 11th, 2001, 02:04 AM
Here how to quick retrieve the content of a file in binary mode

http://codeguru.com/cgi-bin/bbs/wt/showpost.pl?Board=vb&Number=62963&page=&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