Right guys, I'm really new to this but I'm getting along ok, but I've hit a block.

I need to be able to know how to search a text file in VB for a particular line and then display it.
the files I'm trying to use are set up as: Name, number1, number3, number4, number5, number6, number7
like this
methane 50.8 65.3 77.2 0.236 12.0 258.5
So I need to know how to search the file for the Name and then display the rest of that line and save it in array

I can read a file from beginning to end but this I am clueless about, thanks
It's for a system to add, display, update and remove workers
and many thanks in advance

Recommended Answers

All 3 Replies

Use the FileSystemObject that is part of the Microsoft Scripting Runtime (scrrun.dll)

You can use something like -

Public Function ReadTextFileAsString(IN_sFilePath As String) As String
    Dim myFSO As Scripting.FileSystemObject
    Dim myTextStream As Scripting.TextStream
    Dim myString As String

    'Create a new FileSystemObject
    Set myFSO = New Scripting.FileSystemObject

    'Make sure file exists:
    If myFSO.FileExists(IN_sFilePath) Then
        Set myTextStream = myFSO.OpenTextFile(IN_sFilePath, ForReading)
        myString = myTextStream.ReadAll()
        Call myTextStream.Close
    End If
    'Assign Return Value
    ReadTextFileAsString = myString

    'Make sure to clean up when done.
    Set myTextStream = Nothing
    Set myFSO = Nothing
End Function

Also have a look at this link with an attached sample...

thanks AndreRet
but i used the code you wrote lit of error like Type 'Scripting.FileSystemObject' is not defined.
i wrote a code and works fine but always show nothing found!!
so may you please see this code and correct it

Dim A As String = "C:\Users\text.txt"
        Dim objReader As New System.IO.StreamReader(A)
        Dim strStudentName As String
        Dim dteBirthData As String
        Dim strAddress As String
        Dim strSearch As String
        Dim blnFound As Boolean
        Dim strMessage As String
        Dim intStyle As MsgBoxStyle
        strSearch = InputBox("Enter a name to find", "find", , , )
        blnFound = False
        While blnFound = False And objReader.Peek() <> -1
            strStudentName = strStudentName & objReader.Read() & vbNewLine
            If strStudentName = strSearch Then
                dteBirthData = dteBirthData & objReader.Read()
                strAddress = strAddress & objReader.Read()
                blnFound = True
            End If

        End While
        If blnFound Then
            txtStudentName.Text = strStudentName
            txtBirthData.Text = CStr(dteBirthData)
            txtAddress.Text = strAddress
        Else
            strMessage = "Not Found"
            intStyle = vbOKOnly + vbApplicationModal + vbCritical
            MsgBox(strMessage, intStyle, "Not Found")
        End If

@moodylv: You are in the wrong forum. That is VB.Net code.
I will ask a moderator to move this. For future reference, this is the correct forum: http://www.daniweb.com/software-development/vbnet/58

Take a look at this article:

How to: Read From Comma-Delimited Text Files in Visual Basic

In the example change:

MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")

to:

MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(" ") ' your file is space delimited
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.