Search for a text string in text file 
Author Message
 Search for a text string in text file

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



Tue, 15 Oct 2002 03:00:00 GMT  
 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



Tue, 15 Oct 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Working with Text Files - Searching for strings and then exporting to new file

2. Search for and Replace strings in a text file

3. Problem importing text file and searching for strings

4. Function to search text file for given string

5. Slow Searching Text File / String

6. SEARCHING A TEXT FILE FOR A STRING

7. Searching And Replacing Strings Within A Text File

8. search a string in text file

9. Searching And Replacing Strings Within A Text File

10. efficiently searching for text in a text file

11. How to search a text file and replace text

12. How to search and replace text in text file

 

 
Powered by phpBB® Forum Software