Share via


TextFieldParser Object

Provides methods and properties for parsing structured text files.

Public Class TextFieldParser

Exceptions

The following condition may cause an exception:

  • A text field does not match the specified format. For example, in a fixed-width file, one of the fields does not match the specified width. (MalformedLineException).

Remarks

The TextFieldParser object provides methods and properties for parsing structured text files. Parsing a text file with the TextFieldParser is similar to iterating over a text file, while the ReadFields method to extract fields of text is similar to splitting the strings.

The TextFieldParser can parse two types of files: delimited or fixed-width. Some properties, such as Delimiters and HasFieldsEnclosedInQuotes are meaningful only when working with delimited files, while the FieldWidths property is meaningful only when working with fixed-width files.

Tasks

The following table lists examples of tasks involving the Microsoft.VisualBasic.FileIO.TextFieldParser object.

To

See

Read from a delimited text file

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

Read from a fixed-width text file

How to: Read From Fixed-width Text Files in Visual Basic

Read from a text file with multiple formats

How to: Read From Text Files with Multiple Formats in Visual Basic

Example

This example parses through a tab-delimited text file, Bigfile.

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser _
("c:\logs\bigfile")
    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    MyReader.Delimiters = New String() {vbTab}
    Dim currentRow As String()
    'Loop through all of the fields in the file.  
    'If any lines are corrupt, report an error and continue parsing.  
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()
            ' Include code here to handle the row. 
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & _
            " is invalid.  Skipping")
        End Try 
    End While 
End Using

This example depends on the existence of a function, processFields, which processes the fields as they are read.

Requirements

Namespace:Microsoft.VisualBasic.FileIO

Class:TextFieldParser

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Tasks

Troubleshooting Exceptions: Microsoft.VisualBasic.FileIO.TextFieldParser.MalformedLineException

Concepts

Parsing Text Files with the TextFieldParser Object

Reference

TextFieldParser Object Members

TextFieldParser.CommentTokens Property

TextFieldParser.Delimiters Property

TextFieldParser.EndOfData Property

TextFieldParser.ErrorLine Property

TextFieldParser.ErrorLineNumber Property

TextFieldParser.FieldWidths Property

TextFieldParser.HasFieldsEnclosedInQuotes Property

TextFieldParser.LineNumber Property

TextFieldParser.TextFieldType Property

TextFieldParser.TrimWhiteSpace Property

TextFieldParser.Close Method

TextFieldParser.PeekChars Method

TextFieldParser.ReadFields Method

TextFieldParser.ReadLine Method

TextFieldParser.ReadToEnd Method

TextFieldParser.SetDelimiters Method

TextFieldParser.SetFieldWidths Method

My.Computer.FileSystem.OpenTextFieldParser Method

Microsoft.VisualBasic.FileIO.TextFieldParser