VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Tab-delimited file parser This function reads tab-delimited file into datatable using VisualBasic.F

by Bruno Laurinec (1 Submission)
Category: Miscellaneous
Compatability: VB.NET
Difficulty: Unknown Difficulty
Originally Published: Tue 20th October 2009
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Tab-delimited file parser This function reads tab-delimited file into datatable using VisualBasic.FileIO.TextFieldParser class

Rate Tab-delimited file parser This function reads tab-delimited file into datatable using VisualBasic.F




        ' Declare VisualBasic TextFieldParser object
        Dim Parser As Microsoft.VisualBasic.FileIO.TextFieldParser

        ' Give the TextFieldParser object path to the file
        Parser = My.Computer.FileSystem.OpenTextFieldParser(filePath)

        ' Set the field type to delimited
        Parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited

        ' Set the character, that should the textfile be delimited with. As we are going to parse 
        ' tab-delimited file, the delimiter will be Tab
        Parser.SetDelimiters(vbTab)



        ' Declaring the datatable we will put the data into
        Dim dtFinal As New DataTable

        ' Declaration of new DataRow
        Dim dtNewRow As DataRow

        ' Setting-up boolean to know if we are on the first row to know whether add row, or add columns into the table
        Dim FirstRow As Boolean = False
        Dim CurrentRow() As String


        Try

            Do While Not Parser.EndOfData = True


                Try

                    ' Read one parsed line from the file
                    CurrentRow = Parser.ReadFields()

                    ' If we are on the first row, we're gonna be adding columns into the dtFinal datatable
                    If FirstRow = False Then
                        For i As Integer = 0 To UBound(CurrentRow)
                            dtFinal.Columns.Add(CurrentRow(i).ToString, System.Type.GetType("System.String"))
                        Next

                        ' Change value of the boolean so we know, that columns are already added and Continuation of the loop
                        FirstRow = True
                        Continue Do
                    Else

                        ' Adding new row into the DataTable
                        dtNewRow = dtFinal.NewRow
                        For i As Integer = 0 To UBound(CurrentRow)
                            dtNewRow.Item(i) = CurrentRow(i).ToString
                        Next
                        dtFinal.Rows.Add(dtNewRow)
                        dtNewRow = Nothing
                    End If

                    ' To prevent application looking "Not responsing"
                    System.Windows.Forms.Application.DoEvents()

                    ' In case of error, messagebox will be popped-up....this is optional and can be changed to writing message into 
                    ' logfile or something similar..
                Catch ex As Exception
                    MessageBox.Show("ERROR Occured during file processing. The error message is """ & ex.Message & """", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                End Try


            Loop

            ' In case of error, messagebox will be popped-up....this is optional and can be changed to writing message into 
            ' logfile or something similar..
        Catch ex As Exception
            MessageBox.Show("ERROR Occured during file processing. The error message is """ & ex.Message & """", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Finally

            ' Release the file and disposing of the TextFileParser object
            Parser.Close()
            Parser.Dispose()
        End Try


        ' Return the newly constructed datatable
        Return dtFinal


    End Function


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.DataSource = ParseTabDelimited("C:\polart.txt")
    End Sub

Download this snippet    Add to My Saved Code

Tab-delimited file parser This function reads tab-delimited file into datatable using VisualBasic.F Comments

No comments have been posted about Tab-delimited file parser This function reads tab-delimited file into datatable using VisualBasic.F. Why not be the first to post a comment about Tab-delimited file parser This function reads tab-delimited file into datatable using VisualBasic.F.

Post your comment

Subject:
Message:
0/1000 characters