Question : editing CSV file in VB. Visual Studio 2010 used.

I have a CSV file that looks like this:
a,b,c,d,e,f
1,2,3,4,5,6

Can anyone help me figure out a way to eliminate Nth(lets say 4th) column from every line in VB?

I found a way to parse it online which looks like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\test.csv")
            MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
            'set the delimiter to any value
            MyReader.Delimiters = New String() {","}

            Dim currentRow As String()

            'Loop through all of the fields in the file.
            'If any lines are corrupt, report an error and continue parsing.
            While Not MyReader.EndOfData
                Try
                    currentRow = MyReader.ReadFields()
                    'ouput the second value of each line to show that the file has been parsed.
                    currentRow(4)

                    MsgBox(currentRow(4))
                                   Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    MsgBox("Line " & ex.Message & _
                    " is invalid. Skipping")
                End Try

            End While

        End Using

    End Sub

--------------------------

Works pretty well, however, I need to not show this value, but DELETE it.

thank you,

Answer : editing CSV file in VB. Visual Studio 2010 used.

"I need to delete columns 100, 88, and 85 to make format look the same."

You can do it all in the same For Loop:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FileName As String = "c:\test.csv"
        Dim lines() As String = System.IO.File.ReadAllLines(FileName)
        Dim values As New List(Of String)
        For i As Integer = 0 To lines.GetUpperBound(0)
            values.Clear()
            values.AddRange(lines(i).Split(","))

            ' remove all unwanted columns in backwards order 
            ' so you don't have to adjust the column numbers as you go
            values.RemoveAt(99) ' Remove 100th column
            values.RemoveAt(87) ' Remove 88th column
            values.RemoveAt(84) ' Remove 85th column

            lines(i) = String.Join(",", values.ToArray)
        Next
        System.IO.File.WriteAllLines(FileName, lines)
    End Sub
Random Solutions  
 
programming4us programming4us