PDA

Click to See Complete Forum and Search --> : Searching text files for a string


Jan 5th, 2000, 05:48 PM
Is there a function in VB to search a text file for a specified string?

I've tried to input from the text file into a variable then use InStr to compare the two before looping and testing the next input - but it doesn't work...

If no-one has any ideas I can send this code and see if anyone knows what's wrong with it...

Thanks

Jan 5th, 2000, 06:11 PM
Use this code to read in the text file...

Dim sTextFile as string
On Error Resume Next
Open sTextFile For Input As #1
OpenFile = Input(LOF(1), 1)
Close #1


Then use InStr$ to find your text in "sTextFile".

------------------
Matthew Ralston
E-Mail: m.ralston@mediavault.co.uk
ICQ:31422892 (http://www.thebluelink.cjb.net/icq.html)
Web Sites:The Blue Link (http://www.thebluelink.cjb.net) My Home Page (http://mralston.cjb.net)

Jan 5th, 2000, 06:48 PM
This is similar to the code I have used - but the problem with it now is that each time I call Input# it inputs only one line (or something) but I want it to input the entire contents of the file (I know they aren't massive) because I need to search multiple files at a time in a loop...

Also could you explain this line of code:

OpenFile = Input(LOF(1), 1)

Thanks for the help

Jan 5th, 2000, 06:52 PM
That code should load the entire text file into "sTextFile". I've got it in a module that I use regularly...

Option Explicit


Function OpenFile(Filename As String) As String
On Error Resume Next
Open Filename For Input As #1
OpenFile = Input(LOF(1), 1)
Close #1
End Function


Sub SaveFile(Filename As String, Text As String)
On Error Resume Next
Open Filename For Output As #1
Print #1, Text
Close #1
End Sub


------------------
Matthew Ralston
E-Mail: m.ralston@mediavault.co.uk
ICQ:31422892 (http://www.thebluelink.cjb.net/icq.html)
Web Sites:The Blue Link (http://www.thebluelink.cjb.net) My Home Page (http://mralston.cjb.net) (Not up at the moment!)


[This message has been edited by matthewralston (edited 01-06-2000).]

[This message has been edited by matthewralston (edited 01-06-2000).]

[This message has been edited by matthewralston (edited 01-06-2000).]