
Search for a text string in text file
You can read the file into a string:
dim fh as Long
dim sFileName as String
dim sFileContents as String
dim sTemp as String
sFileName = "C:\Whatever.txt"
'Import the entire file into a string
fh = FreeFile()
Open sFileName For Input As #fh
Do While Not EOF(fh)
Line Input #fh, sTemp
sFileContents = sFileContents & sTemp & vbCrLf
Loop
Close #fh
And then search the string using InStr(). Also, instead of looping through
the file line by line, I generally use this syntax:
sFileContents = Input(LOF(fh), #fh)
But there's a bug that causes an error sometimes, so you may want to just
stick with the loop in the first example.
--
Regards,
Gregory Silvano
http://www.codehound.com
Free Internet Search Engine for VB Developers
Quote:
> Hie!
> How can I search for a text string in a file. e.g. I want to look for the
> name "Otto" in a textfile "Names.txt" which could have a lot of names in
it.
> Thanks in advance!
> Otto