Solved

How to delete CR LF in a .csv file using Vb.Net

Posted on 2014-09-23
6
778 Views
Last Modified: 2014-09-23
I am working on VS 2012, Vb.Net - .Net framework 4.0

This piece of code, read the .csv file and expected to rewrite the .csv file without Carriage Return.

But now, Carriage Return is created as a new line. I have also attached the screen shot.

Along with the post, i have attached the sample csv file.

 Public Sub Test()
        Try

            Dim reader As StreamReader = New System.IO.StreamReader(File.OpenRead("D:\CSV\Test.csv"))
            Dim listA As New List(Of String)()


            If File.Exists("d:\CSV\TestOut1.csv") Then
                File.Delete("d:\CSV\TestOut1.csv")
            End If

            Dim sw As New StreamWriter("d:\CSV\TestOut1.csv")
            Dim s As String = String.Empty

            While reader.Peek() >= 0
                Dim line As String = reader.ReadLine()
                Dim values As String() = line.Split(";"c)
                listA.Add(values(0))
                s = s + line + Chr(10)
            End While
            reader.Close()
            sw.WriteLine(s)
            sw.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    End Sub

Open in new window

D--CSV-Expected-Output.jpg
D--CSV-Test.csv
0
Comment
Question by:chokka
6 Comments
 
LVL 9

Expert Comment

by:skipper68
ID: 40339633
have you tried .replace(vbcrlf, "") on the 'line' or the split array of items?
Dim values As String() = line.Split(";"c).replace(vbCrLf,"")
or
listA.Add(values(0).replace(vbCrLf,""))
0
 
LVL 14

Expert Comment

by:Don Thomson
ID: 40339639
search for the ascii character combination of CRLF  (13 and 10) and and don't write a new line

Or simply search for a  line with a length of less than 3 characters and ignore it

Not sure why your doing this - a .csv  file will always be created with the CRLF  at the end.
Every program that I've seen that imports a CSV file uses the CRLF as an end of file and stops writing
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40339670
Change this line of code

sw.WriteLine(s)

to this

sw.Write(s)

And see if that is what you are looking for.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:chokka
ID: 40339840
@Fernando, Great !! Thank you !! Looks like it works !!

I need to test again to confirm
0
 

Author Closing Comment

by:chokka
ID: 40340482
Thank you !!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40340542
Not a problem, glad I was able to help.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question