Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search for string in text file

Status
Not open for further replies.

angela4eva

Programmer
Joined
Apr 29, 2005
Messages
46
Location
US
Hi would appreciate if some one could help me with fatest way to search for a string "asf0798" in my text file data.dat and return a yes or no value(i.e the string exists or not..Some one please help with the fatest way to do this.
thanks
 
Dim oFile as System.IO.File
Dim oRead as System.IO.StreamReader
dim found as boolean
found = false
oRead = oFile.OpenText(“C:\data.dat”)
While oRead.Peek <> -1 or found
if instr(oRead.ReadLine(),"asf0789") <> 0 then
found = true
end if
End While

oRead.Close()

Something like this ?
Do not forget to close the streamreader !!!
 
Read the file into a string and then use a RegEx (Regular expresion).

If you just have some files you need to check quickly (ie this is not part of an app you're trying to create) take a look at grep on unix boxes - versions are available (for free) on Windows.

mmilan.
 
This works in the 2.0 framework:

Code:
if io.File.ReadAllText("C:\MyFile.dat").Contains("asf0798") then 
  msgbox("String found!")
else
  msgbox("String not found!")
end if

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
nice solution Rick, but i think it takes longer reading the whole file. This depends of course on the file size, but I see it this way. why read an entire textfile when the line could be found on rule 3.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top