Parsing Multiple Lines

Gluttard

Active member
Joined
Jan 9, 2008
Messages
44
Programming Experience
Beginner
Hello, I'm working on a software project that must read 'log' files and parse each line. Right now I'm just doing a proof of concept type thing, and trying to read two lines of a text file with "," as delimiters.

Here's what I have so far:
PHP:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName <> "" Then
            Dim LogFile As String = OpenFileDialog1.FileName
            ListBox1.Items.Clear()
            Dim Parser As Microsoft.VisualBasic.FileIO.TextFieldParser
            Parser = My.Computer.FileSystem.OpenTextFieldParser(LogFile)
            Parser.SetDelimiters(",")
            Dim textfields As String() = Parser.ReadFields()

            For Each currentfield As String In textfields
                ListBox1.Items.Add(currentfield)
            Next
            Parser.Close()
        End If

And say I'm trying to read a file with this in it:
Can,You,Read
THIS,Sentence?

I choose the file to read, it only comes up with the first line. I have a feeling I can do something with line number, but the code I tried didn't work right.
Thanks.
 
This is what you get when inserting the snippet: Fundamentals, File system, Read a delimited file:
VB.NET:
Dim filename As String = "C:\Test.txt"
Dim fields As String()
Dim delimiter As String = ","
Using parser As New TextFieldParser(filename)
    parser.SetDelimiters(delimiter)
    While Not parser.EndOfData
        ' Read in the fields for the current line
        fields = parser.ReadFields()
        ' Add code here to use data in fields variable.

    End While
End Using
 
A rookie way, doesnt support quoting of fields:

For Each line as String in File.ReadAllLines("c:\test.txt")
Dim bits() as String = line.Split(","c)
 
Back
Top