Results 1 to 3 of 3

Thread: [RESOLVED] VB.net delete rows from .csv where value = 0

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Resolved [RESOLVED] VB.net delete rows from .csv where value = 0

    I am trying to delete all rows from a .csv where a column is 0.

    I use a streamreader to read the lines and then streamwriter to create a new file.

    However the rows are not being deleted.

    Code:
    Dim SR As New StreamReader(extractPath & GebruikHierdieCSV & ".csv")
            Dim SWriter As New StreamWriter(extractPath & "nuwe verkorte csv file.csv")
            Dim strLineData() As String
    
            Do While SR.Peek <> -1
    
                strLineData = Split(SR.ReadLine, ",")
    
                If strLineData(16) <> "0" Then
    
                    SWriter.WriteLine(strLineData(0) & "," & strLineData(1) & "," & strLineData(3) & "," & strLineData(6) & "," & strLineData(9) & "," & strLineData(10) & "," & strLineData(11) & "," & strLineData(13) & "," & strLineData(16) & "," & strLineData(18))
                End If
            Loop
    
            SR.Close()
            SWriter.Close()
    
            MessageBox.Show("File has been created and can be located at " & extractPath & "nuwe verkorte csv file.csv")

    I only write certain columns to the new .csv file.

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

    Re: VB.net delete rows from .csv where value = 0

    That code doesn't delete anything. What it does is read data and then write it to a new file and filter out some of the original data. Are you actually saying that every line from the original file has a corresponding line in the new file?

    Have you actually debugged the code? I can only assume that you haven't because any issues would be obvious if you had. Set a breakpoint and step through the code line by line and actually look at the data you're processing as you process it. If you don't know how to use the debugger then stop what you're doing and learn now.

    Apart from using the tools provided by your IDE to actually do development, you can clean that code up considerably too:
    vb.net Code:
    1. Using writer As New StreamWriter(Path.Combine(extractPath, "nuwe verkorte csv file.csv")
    2.     For Each line In File.ReadLines(Path.Combine(extractPath, GebruikHierdieCSV & ".csv")
    3.         Dim fields = line.Split(","c)
    4.  
    5.         If fields(16) <> "0" Then
    6.             writer.WriteLine($"{strLineData(0)},{strLineData(1)},{strLineData(3)},{strLineData(6)},{strLineData(9)},{strLineData(10)},{strLineData(11)},{strLineData(13)},{strLineData(16)},{strLineData(18)}")
    7.         End If
    8.     Next
    9. End Using
    It's worth noting that both your original code and this code will not account for data that contains columns within the data. If that's something you need, you should look at using a TextFieldParser instead.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Re: VB.net delete rows from .csv where value = 0

    Thank you jmchilhinney.

    Your code is much cleaner than mine.

    Your question
    Are you actually saying that every line from the original file has a corresponding line in the new file?
    provided the answer.

    The solution was simple really. The data in the .csv is "0.0000" and not "0".

    So changing the line
    Code:
    If fields(16) <> "0.0000" Then
    did the trick.

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