Results 1 to 8 of 8

Thread: Remove empty columns from a tab delimited text file?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Question Remove empty columns from a tab delimited text file?

    How can I open a tab delimited text file and remove the columns that are empty and the save it?
    Empty columns are those that have no value in the first row.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Remove empty columns from a tab delimited text file?

    I'd suggest reading the data into a DataTable, testing the data in the first row, removing the appropriate DataColumns, then writing the data back out to the file. You could use ADO.NET to read the data, or a TextFieldParser, or a StreamReader. I'd go with the TextFieldParser. You'd then use a StreamWriter to write the data back out again in a nested For or For Each loops.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Remove empty columns from a tab delimited text file?

    Thanks jmcilhinney,

    Do you have any examples you can show?

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Remove empty columns from a tab delimited text file?

    Ok, this imports a text file to its data table.
    But how can I delete these empty columns? And how can I then export back the text file? Can I export with other delemiters also?
    vb.net Code:
    1. Private Function BuildDataTable(ByVal fileFullPath As String, ByVal seperator As Char) As DataTable
    2.  
    3.         Dim myTable As DataTable = New DataTable("MyTable")
    4.         Dim i As Integer
    5.         Dim myRow As DataRow
    6.         Dim fieldValues As String()
    7.         Dim f As IO.File
    8.         Dim myReader As IO.StreamReader
    9.         Try
    10.             'Open file and read first line to determine how many fields there are.
    11.             myReader = f.OpenText(fileFullPath)
    12.             fieldValues = myReader.ReadLine().Split(seperator)
    13.             'Create data columns accordingly
    14.             For i = 0 To fieldValues.Length() - 1
    15.                 myTable.Columns.Add(New DataColumn("Field" & i))
    16.             Next
    17.             'Adding the first line of data to data table
    18.             myRow = myTable.NewRow
    19.             For i = 0 To fieldValues.Length() - 1
    20.                 myRow.Item(i) = fieldValues(i).ToString
    21.             Next
    22.             myTable.Rows.Add(myRow)
    23.             'Now reading the rest of the data to data table
    24.             While myReader.Peek() <> -1
    25.                 fieldValues = myReader.ReadLine().Split(seperator)
    26.                 myRow = myTable.NewRow
    27.                 For i = 0 To fieldValues.Length() - 1
    28.                     myRow.Item(i) = fieldValues(i).ToString
    29.                 Next
    30.                 myTable.Rows.Add(myRow)
    31.             End While
    32.         Catch ex As Exception
    33.             MsgBox("Error building datatable: " & ex.Message)
    34.             Return New DataTable("Empty")
    35.         Finally
    36.             myReader.Close()
    37.         End Try
    38.  
    39.         Return myTable
    40.     End Function
    Call it with
    vb.net Code:
    1. BuildDataTable("C:\Testfile.txt", vbTab)
    Last edited by Pirre001; Dec 14th, 2009 at 04:23 AM.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Remove empty columns from a tab delimited text file?

    Someone...?

  6. #6
    Junior Member
    Join Date
    Nov 2009
    Location
    UK
    Posts
    16

    Re: Remove empty columns from a tab delimited text file?

    You added columns with myTable.Columns.Add
    You should be able to drop the ones you don't want with Remove, e.g. to drop column 3, myTable.Columns.Remove(3)

    Remember they count from 0, of course.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2002
    Posts
    665

    Re: Remove empty columns from a tab delimited text file?

    Ok, but how can I drop empty columns? What I mean is, how can I figure out wich columns who are empty? That's the problem...

  8. #8
    Junior Member
    Join Date
    Nov 2009
    Location
    UK
    Posts
    16

    Re: Remove empty columns from a tab delimited text file?

    You defined an empty column as "Empty columns are those that have no value in the first row.". So you need to test every field in the first row to see if it's empty (Null).

    Something like
    Code:
    myRow = myTable.Rows(0)
        For i = myTable.Columns.Count - 1 To 0 Step -1
        If myRow.Item(i).isNull Then myTable.Columns.Remove(i)
    Next i
    ought to work (no I have not tested it).

    Note that it's stepping down from the last column, so it only removes columns you've already checked.

    There are ways to solve your problem without building tables at all. You could read in the first row, set an array GoodCol() as Boolean to True or False depending whether each field is empty, then read in and split each row, writing out only the fields for which GoodCol(i) was true.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width