How to: Read From Text Files with Multiple Formats in Visual Basic
The TextFieldParser object provides a way to easily and efficiently parse structured text files, such as logs. You can process a file with multiple formats by using the PeekChars method to determine the format of each line as you parse through the file.
To parse a text file with multiple formats
Define the expected format and the format used when an error is reported.
Dim StdFormat As Integer()= {5,10,11,-1} Dim ErrorFormat As Integer() = {5,5,-1}
Create a new TextFieldParser object, defining the width and format.
Using MyReader As New _ Microsoft.VisualBasic.FileIO.TextFieldParser("C:\testfile.txt") MyReader.TextFieldType = FileIO.FieldType.FixedWidth
Loop through the rows, testing for format before reading.
Dim CurrentRow As String() While Not MyReader.EndOfData Try Dim RowType As String = MyReader.PeekChars(3) If String.Compare(RowType, "Err") = 0 Then ' If this line describes an error, the format of ' the row will be different. MyReader.SetFieldWidths(ErrorFormat) CurrentRow = MyReader.ReadFields MyReader.SetFieldWidths(StdFormat) Else 'Otherwise parse the fields normally CurrentRow = MyReader.ReadFields For Each newString As String In CurrentRow My.Computer.FileSystem.WriteAllText _ ("newFile.txt", newString, True) Next End If
Write errors to the console.
Catch ex As _ Microsoft.VisualBasic.FileIO.MalformedLineException MsgBox("Line " & ex.Message & " is invalid.") End Try End While End Using
Example
This example reads from the file testfile.txt.
Dim StdFormat As Integer() = {5, 10, 11, -1}
Dim ErrorFormat As Integer() = {5, 5, -1}
Using MyReader As New _
Microsoft.VisualBasic.FileIO.TextFieldParser("C:\testfile.txt")
MyReader.TextFieldType = FileIO.FieldType.FixedWidth
MyReader.FieldWidths = StdFormat
Dim CurrentRow As String()
While Not MyReader.EndOfData
Try
Dim RowType As String = MyReader.PeekChars(3)
If String.Compare(RowType, "Err") = 0 Then
' If this line describes an error, the format of the row will be different.
MyReader.SetFieldWidths(ErrorFormat)
CurrentRow = MyReader.ReadFields
MyReader.SetFieldWidths(StdFormat)
Else
' Otherwise parse the fields normally
CurrentRow = MyReader.ReadFields
For Each newString As String In CurrentRow
My.Computer.FileSystem.WriteAllText("newFile.txt", newString, True)
Next
End If
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using
Robust Programming
The following conditions may cause an exception:
A row cannot be parsed using the specified format (MalformedLineException). The exception message specifies the line causing the exception, while the TextFieldParser.ErrorLine Property is assigned to the text contained in the line.
The specified file does not exist (FileNotFoundException).
A partial-trust situation in which the user does not have sufficient permissions to access the file. (SecurityException).
The path is too long (PathTooLongException).
The user does not have sufficient permissions to access the file (UnauthorizedAccessException).
See Also
Tasks
How to: Read From Comma-Delimited Text Files in Visual Basic
How to: Read From Fixed-width Text Files in Visual Basic
Concepts
Parsing Text Files with the TextFieldParser Object
Reference
TextFieldParser.PeekChars Method
My.Computer.FileSystem.WriteAllText Method