hi, im a new comer in this forum.
please anyone can help me for find string in large text file (filesize = 1mb to 5 mb)

example: i have a text document file, filename is test.txt
file test.txt contains serial number

000001
000002
000003
000004
000005
000006
000007
000008
000009
000010

The question is what is a code to find that serial number, and if not match will show message box ("Serial number not found") and if found will show message box ("Serial number found")

i also try this code but not working good, because
when i type textbox1.text = 0001
message box show "Serial number found"
i want message box show "Serial number not found" because serial number in test.txt dont have "0001"

this is the code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If FindStringInFile("c:\test.txt", textbox1.text) Then
MessageBox.Show("Serial number found")
Else
MessageBox.Show("Serial number not found")
End If
End Sub
Public Function FindStringInFile(ByVal Filename As String, ByVal SearchString As String) As Boolean
Dim Reader As System.IO.StreamReader
Reader = New IO.StreamReader(Filename)
Dim stringReader As String
Try
While Reader.Peek <> -1
stringReader = Reader.ReadLine()
If InStr(stringReader, SearchString) > 0 Then Return True
End While
Reader.Close()
Return False
Catch ex As Exception
MessageBox.Show("Exception: " & ex.Message)
Return False
End Try
End Function

source: http://www.daniweb.com/software-development/vbnet/threads/75222/search-text-in-a-doc-file

maybe all of you guys can help me to write a correct code for what i mean.
thanks for you help and your time.

Recommended Answers

All 3 Replies

The following code will extract all lines in a file containing a given string. If Ubound(lines) is -1 then the string wasn't found.

    Dim file As String = "D:\temp\swap.txt"
    Dim text As String = "Aaaaa"
    Dim lines() As String = Filter(System.IO.File.ReadAllLines(file), text)

Hi Reverend Jim, Thanks for your reply.
Did you mean like this?

    Dim file As String = "c:\data.txt"
    Dim text As String = TextBox1.Text
    Dim lines() As String = Filter(System.IO.File.ReadAllLines(file), text)
    If UBound(lines) = -1 Then
        MsgBox("not found")
    Else
        MsgBox("found")
    End If

Maybe i wrong write the code, because not working good. The text file contains:
000001
000002
000003
000004
000005
000006
000007
000008
000009
000010

when i type textbox1.text = 0001
message box show "found"
i want message box show "not found" because serial number in test.txt dont have "0001" but "000001".

Please i need help.

0001 is in the file because it is contained in the string 000001. You could do a loop over the matching lines to see if there is an exact match.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.