CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2001
    Location
    South Korea
    Posts
    5

    Delete line feeds in text files

    Hello,

    I've searched, and I've searched. Does anyone know an easy way to delete line feeds and/or carriage returns in a text file before Line Output to another text file (which I do before importing via code into Excel or Access?)

    The program I am currently working looks like this. I've been trying to delete as much extraneous stuff from a print-to-file to
    the other text file as possible, but I still
    have blank lines.

    Sub A10_PullFromControlOfHoursReport()
    '
    ' A01_PullFromControlOfHoursReport Macro
    ' Macro recorded 4/25/01 by Kathy DuBach
    '
    Dim strInput As String
    Open "C:\MyDocs\Payroll\Pay_DCPS\OrgCd_LineNos\Printfil.001" For Input As 1
    Open "C:\MyDocs\Payroll\Pay_DCPS\OrgCd_LineNos\OrgCd_LineNos.txt" For Output As 2
    Do Until EOF(1)
    Line Input #1, strInput
    If Mid(strInput, 4, 5) <> "START" And _
    Mid(strInput, 9, 1) <> " " And _
    Mid(strInput, 1, 1) <> "" And _
    Mid(strInput, 9, 5) <> "TOTAL" Then
    Print #2, strInput
    End If
    Loop
    Close
    End Sub





    Kathy DuBach at [email protected]

  2. #2
    Join Date
    Feb 2000
    Location
    Ireland
    Posts
    808

    Re: Delete line feeds in text files

    you can use the replace function
    mystring=replace(mystring,vbCR,"")
    and the same for line feeds
    mystring=replace(mystring,vbLF,"")



  3. #3
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Delete line feeds in text files

    If you put a semi-Colon following your print statement, VB will not append a CRLF to the end of the line being printed. Below sample illustrates this. You will of course need to change the location of the output file

    option Explicit

    private Sub Command1_Click()
    Dim X
    Open "C:\VB Stuff\Things to look at\Junk.txt" for Output as #1
    for X = 0 to 10
    print #1, " Item " & X; ' Trailing semiColon
    next X
    Close
    Unload me
    End Sub




    John G

  4. #4
    Join Date
    Apr 2001
    Location
    South Korea
    Posts
    5

    Re: Delete line feeds in text files

    Thanks so much. The ";" at the end of Print #2, strInput; worked for the carriage returns and line feeds. Then I discovered I also had form feeds (CHR(12) to get rid of. Fortunately, I didn't want any data in those fields.

    Sub A10_PullFromControlOfHoursReport()
    '
    ' A01_PullFromControlOfHoursReport Macro
    ' Macro recorded 4/25/01 by Kathy DuBach
    '
    Dim strInput As String
    Open "C:\MyDocs\Payroll\Pay_DCPS\OrgCd_LineNos\Printfil.001" For Input As 1
    Open "C:\MyDocs\Payroll\Pay_DCPS\OrgCd_LineNos\OrgCd_LineNos.txt" For Output As 2
    Do Until EOF(1)
    Line Input #1, strInput
    If Mid(strInput, 1, 1) <> Chr(12) And _
    Mid(strInput, 1, 1) <> "" And _
    Mid(strInput, 1, 1) <> ["*"] And _
    Mid(strInput, 1, 6) <> "REPORT" And _
    Mid(strInput, 4, 5) <> "START" And _
    Mid(strInput, 9, 1) <> " " And _
    Mid(strInput, 9, 5) <> "TOTAL" And _
    Mid(strInput, 61, 4) <> "R7.0" Then
    strInput = RTrim(strInput)
    Print #2, strInput;
    End If
    Loop
    Close
    End Sub


    Kathy DuBach at [email protected]

  5. #5
    Join Date
    Apr 2001
    Location
    South Korea
    Posts
    5

    Re: Delete line feeds in text files

    I tried Replace several different ways. I'm using Excel VBA. Is it possible? How would the
    line fit within my abbreviated program below?Sub A10_PullFromControlOfHoursReport()
    '
    ' A01_PullFromControlOfHoursReport Macro
    ' Macro recorded 4/25/01 by Kathy DuBach
    '
    Dim strInput As String
    Open "C:\MyDocs\Payroll\Pay_DCPS\OrgCd_LineNos\Printfil.001" For Input As 1
    Open "C:\MyDocs\Payroll\Pay_DCPS\OrgCd_LineNos\OrgCd_LineNos.txt" For Output As 2
    Do Until EOF(1)
    Line Input #1, strInput
    If Mid(strInput, 1, 1) <> Chr(12) And _
    Mid(strInput, 61, 4) <> "R7.0" Then
    strInput = RTrim(strInput)
    Print #2, strInput
    End If
    Loop
    Close
    End Sub



    Kathy DuBach at [email protected]

  6. #6
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Delete line feeds in text files

    Glad I could help. I don't know where the FromFeed is coming from, VB doesn't do that one but they can be gotten rid of by using the Replace function like so

    private Sub Command1_Click()
    Dim strStr as string
    strStr = "Hello" & Chr(12) & " There"
    print strStr
    strStr = Replace(strStr, Chr(12), " ", 1)
    print strStr
    End Sub




    John G

  7. #7
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Delete line feeds in text files

    See my latest append for a sample of Replace function and put the Replace immediately after your Line Input like so

    strInput = Replace(strInput,chr(12)," ",1)




    John G

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

Featured