Home > Software design >  How to remove extra line feeds within double quoted fields
How to remove extra line feeds within double quoted fields

Time:09-22

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

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

CodePudding user response:

If you are sure there are no double quotes text inside the double quotes you can do it like this:

Dim sNewString As String = ""
Dim s As String
Dim bFirstQuoted As Boolean = False
Dim i As Integer

Dim objWriter As New System.IO.StreamWriter("testfile.csv")
MsgBox(contents)

For i = 1 To contents.Length
    s = Mid(contents, i, 1)
    If s = """" Then bFirstQuoted = Not bFirstQuoted
    If Not bFirstQuoted OrElse (s <> vbLf AndAlso bFirstQuoted) Then
        sNewString  = s
    End If
Next

MsgBox(sNewString )
objWriter.Write(sNewString )
objWriter.Close()
Dts.TaskResult = ScriptResults.Success
  • Related