Thahir Hussain

Thahir Hussain

  • 1.6k
  • 95
  • 9.7k

txt file to datatable with 2 spaces

Feb 20 2024 5:55 AM

Hello

I havw a txt file generated from barcode machine, where in one column i get vehicle model code with 3 char and sometime 2 char and next column vinnumber with 6 char.

If it modelcode has 3 char there is no problem where i have only one space between 2 column

If modelcode 2 char there i am getting 2 spaces between columns and it takes empty records.

Is there any way i can add data if there is 2 spaces.

Attached my sample data and my code

TJW 101555,1
TJW 102009,1
TJW 101335,1
S9  447141,1
S9  447112,1
S9  447175,1
S9  447138,1
S9  447096,1
S9  447105,1
G6  183530,1
G6  183371,1
G6  183372,1
G6  158771,1
G6  183532,1
G6  183531,1
HD  725571,1
TJW 101688,1
TJW 104346,1
TJW 101547,1

 

Dim dt As New DataTable
            dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Model_Code", GetType(String)), New DataColumn("Model_no", GetType(String)), New DataColumn("Model_Name", GetType(String))})
            Dim TxtData As String = File.ReadAllText(TxtPAth)
            For Each Row As String In TxtData.Split(ControlChars.CrLf.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
                If Not String.IsNullOrEmpty(Row) Then
                    dt.Rows.Add()
                    Dim i As Integer = 0
                    'For Each cell As String In Row.Split(" "c)
                    For Each cell As String In Row.Split(" "c) ', StringSplitOptions.RemoveEmptyEntries)
                        dt.Rows(dt.Rows.Count - 1)(i) = LTrim(RTrim(cell))
                        i += 1
                    Next
                End If
            Next


Answers (2)