Solved

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

Posted on 2014-09-23
6
741 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 62

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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 62

Expert Comment

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now