Question Removing extra line feeds in double quoted fields

jbpm

New member
Joined
Sep 21, 2022
Messages
2
Programming Experience
1-3
Newbie here. Code below removes ALL line feeds in my file but it also removes EOR line feeds. Can somebody please help me how to fix code below so it only removes extra line feeds within double quoted fields? Any help will be greatly appreciated. Thanks

VB.NET:
    Public Sub Main()
        '
        Dim objReader As IO.StreamReader
        Dim contents As String

        objReader = New IO.StreamReader("testfile.csv")
        contents = objReader.ReadToEnd()
        objReader.Close()

        Dim objWriter As New System.IO.StreamWriter("testfile.csv")
        MsgBox(contents)
        'contents = Replace(contents, vbCr, "")
        contents = Replace(contents, vbLf, "")
        MsgBox(contents)
        objWriter.Write(contents)
        objWriter.Close()
        '
        Dts.TaskResult = ScriptResults.Success
    End Sub
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,779
Location
Sydney, Australia
Programming Experience
10+
You can use a TextFieldParser to read the data as records, even if it contains line breaks. You can then remove line breaks from individual fields. You'd then have to reconstruct the record, adding in appropriate quotes. E.g.
VB.NET:
Dim lines As New List(Of String)

Using parser As New TextFieldParser(filePath)
    Do Until parser.EndOfData
        Dim fields = parser.ReadFields()
        Dim line = $"""{String.Join(""",""", fields.Select(Function(s) s.Replace(Environment.NewLine, " ")))}"""

        lines.Add(line)
    Loop
End Using

File.WriteAllLines(filePath, lines)
 
Top Bottom