Page 1 of 1

TextFieldParser How to use it. (Eg CSV Files) Rate Topic: ***** 1 Votes

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Post icon  Posted 28 March 2009 - 03:42 PM

Text Field Parser

The TextFieldParser helps you parses files that have predefine fields.
It can handle two types
  • Fixed Length Fields - Where each field is a different section of on a single string.
  • Delimited Fields - Where the fields are separated by a token (usually a comma).
Creating the two test files
Create a new folder in your Solution Explorer called Files.
In that folder create the following text files.
The first is TestFile01.txt, this is the fixed-length one.
' This line is comment
' Field Structure
'  0 - 3  Id
'  4 - 23 Name
' 24 - 31 Date Of Birth
'		 1		 2		 3
'1234567890123456789012345678901
4567John Doe			02181999
8901Jane Smith Doe	  03041972


The Second is TestFile02.txt, this the Separated Fields File.
' This line is comment
' Field Structure
' Id, Name , Date of Birth
' Note: I treat them as strings
"4567","John Doe","02/18/1999"
"8901","Jane Smith Doe","03/04/1972"


Set the Copy to Output Directory property of both files to Copy Always. This is to make sure we have to file in project so we can load them later.

Add new class to the Project call Person, give it following code.
Public Class person
 Private m_ID As String
 Public Property ID() As String
  Get
   Return m_ID
  End Get
  Set(ByVal value As String)
   m_ID = value
  End Set
 End Property
 Private m_Name As String
 Public Property Name() As String
  Get
   Return m_Name
  End Get
  Set(ByVal value As String)
   m_Name = value
  End Set
 End Property
 Private m_DOB As String
 Public Property DOB() As String
  Get
   Return m_DOB
  End Get
  Set(ByVal value As String)
   m_DOB = value
  End Set
 End Property
End Class



The UI
This will be a very simple UI
Add a ListView object to the form set its View to Details
Add the the three columns to it, with column texts of (ID, Name, DoB)
Paste a copy of this listview on the form.
Now on to the Form Code.
Public Class Form1
 Dim A_List As New List(Of person)
 Dim B_List As New List(Of person)
End Class



In the Form1's load event
 From_FixedLengthFile()
  AddPersonsToListview(A_List, Me.ListView1)
  From_DelimitedFile()
  AddPersonsToListview(B_List, Me.ListView2)


Below this add the follow helper routines.
 Private Sub AddPersonsToListview(ByRef pl As List(Of person), ByRef lv As ListView)
  lv.Items.Clear()
  Dim clv As ListViewItem
  For Each p As person In pl
   clv = lv.Items.Add(p.ID)
   clv.SubItems.Add(p.Name)
   clv.SubItems.Add(p.DOB)
  Next
 End Sub
 Public Function ToDate(ByVal d As String) As Date
  Dim rm As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(d, "([0-9]{2})([0-9]{2})([0-9]{4})")
  If rm.Success = False Then Return Nothing
  Dim dr As New Date(CInt(rm.Groups(3).Value), CInt(rm.Groups(1).Value), CInt(rm.Groups(2).Value))
  Return dr
 End Function



Finally the code to read the files.


A Fixed-Length Fields File

 Private Sub From_FixedLengthFile()
  ' Define a new TextFieldParser, passing it our file
  Dim tfp As New FileIO.TextFieldParser(My.Application.Info.DirectoryPath & "\Files\TestFile01.txt")
  ' Tell the TextFieldParse to expect a FixedWidth file (By Default it Delimited)
  tfp.TextFieldType = FileIO.FieldType.FixedWidth
  ' Define an array containing the length of each field, in the order they appear
  Dim FieldLengths() As Integer = {4, 20, 8}
  ' Define an array containing the token to indicate what follows next (to the line end) is a comment
  Dim Comments() As String = {"'"}
  ' Tell our TextFieldParser what the parameters are.
  tfp.FieldWidths = FieldLengths
  tfp.CommentTokens = Comments
 ' Empty the collection of people
  A_List.Clear()
  Dim p As person
  ' Read the file in Try-Catch Block in case of file problems
  Try
   ' Have we read to the end of the file?
   While tfp.EndOfData = False
	' Get the fields of the current line, it automatically moves to the next position.
	Dim FoundField() As String = tfp.ReadFields
	' Does it contain the expected number of fields?
	If FoundField.Count <> 3 Then
	 ' Nope, Display a message.
	 MessageBox.Show(String.Format("Incorrect Number field on line {0}", tfp.LineNumber), "", MessageBoxButtons.OK)
	Else
	 ' Yes, Create a new person object and fill in the details
	 p = New person
	 p.ID = FoundField(0)
	 p.Name = FoundField(1)
	 p.DOB = ToDate(FoundField(2))
	 ' Add the person to our collection of people.
	 A_List.Add(p)
	End If
   End While
  Catch ex As Exception
   Throw ex
  End Try
  ' Remember to close the parser.
  tfp.Close()
 End Sub



A Delimited Fields File
 Private Sub From_DelimitedFile()
  ' Define a new TextFieldParser, passing it our file
  Dim tfp As New FileIO.TextFieldParser(My.Application.Info.DirectoryPath & "\Files\TestFile02.txt")
  ' Tell the TextFieldParse to expect a Delimited file.
  tfp.TextFieldType = FileIO.FieldType.Delimited
  ' Define an array containing the separation token(s) Eg. Comma ","
  Dim Delimiters() As String = {","}
  ' Define an array containing the token to indicate what follows next (to the line end) is a comment
  Dim Comments() As String = {"'"}
  ' Tell our TextFieldParser what the parameters are.
  tfp.Delimiters = Delimiters
  tfp.CommentTokens = Comments
  ' If a field entry with a delimiter or comment token is allow enclose the field in quotes.
  tfp.HasFieldsEnclosedInQuotes = True
  ' Empty the collection of people
  B_List.Clear()

  Dim p As person
  ' Read the file in Try-Catch Block in case of file problems
  Try
   ' Have we read to the end of the file?
   While tfp.EndOfData = False
    ' Get the fields of the current line, it automatically moves to the next position.
    Dim FoundField() As String = tfp.ReadFields
    ' Does it contain the expected number of fields?
    If FoundField.Count <> 3 Then
     ' Nope, Display a message.
     MessageBox.Show(String.Format("Incorrect Number field on line {0}", tfp.LineNumber), "", MessageBoxButtons.OK)
    Else
     ' Yes, Create a new person object and fill in the details
     p = New person
     p.ID = FoundField(0)
     p.Name = FoundField(1)
     p.DOB = DateTime.ParseExact(FoundField(2), "MM/dd/yyyy", Nothing)
     ' Add the person to our collection of people.
     B_List.Add(p)
    End If
   End While
  Catch ex As Exception
   Throw ex
  End Try
  ' Remember to close the parser.
  tfp.Close()
 End Sub



You'll notice the code is very similar to the Fixed-Length one.

That the basic of the TextFieldParser.

For the Adventurous
See what is possible to do with TextFieldParse and a little ingenuity.
Simple Scripting Language.
Simple Scripting Language Cont.

Is This A Good Question/Topic? 1
  • +

Replies To: TextFieldParser

#2 Ghoztrider  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 28-March 11

Posted 25 January 2012 - 05:48 AM

Awesome....thanks Adam!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1