Solved - How to take a text file and delete the CRLFs in the file

Posted on 2005-05-17
Last Modified: 2010-04-17

I am using VS.NET to create a small module program that needs to take a text file and delete all the CRLFs in the file. is the language I'm using.  I've searched through the questions in EE and haven't really found anything that allows me to do this.

As well, I need to delete the first couple lines as well as the last couple lines of the text file.  I have everything built except the editing of the text file itself - I'm stumped on how to do this.  Again, this is a simple .txt file that I need to edit.  Thank you!
Question by:gderoeck
    LVL 1

    Expert Comment

    Maybe try replacing the chr(13)  with a "".  You may have to read each line of the text file at a time.  

    While looping through  each line you can also delete the first 2 and last 2 lines.
    LVL 1

    Author Comment

    Can you post some code to show this?  I'm trying to do what you suggested, but not having much luck.
    LVL 1

    Accepted Solution

    This will read in a file and save it to a temp file.  Where you read in teh line, you will replace the return character with nothing.

    The return character can be masked as vblf, vbnewline or chr(13).  You can try removing all three.

    Hope this helps

     Private Sub CreateTmpFile(ByVal FilePath As String, ByVal tmp_filename As String)
                    '   Declares Variables
                    Dim tmp_writer As StreamWriter
                    Dim tmp_reader As StreamReader
                    '   Creates a New Temp File
                    If File.Exists(FilePath & "\" & tmp_filename & ".tmp") Then
                        File.Delete(FilePath & "\" & tmp_filename & ".tmp")
                    End If
                    tmp_writer = File.CreateText(FilePath & "\" & tmp_filename & ".tmp")

                    '   Opens the TXT file to be read
                    tmp_reader = New StreamReader(FilePath & "\" & Filename)

                    '   Writes old file to the new file
                    Do While tmp_reader.Peek() >= 0

                    '   Cleans up Variables
                    FilePath = Nothing
                    tmp_filename = Nothing
                    Filename = Nothing
                    tmp_reader = Nothing

                    tmp_writer = Nothing
                End Sub
    LVL 1

    Author Comment

    Excellent!  Thank you!
    LVL 1

    Expert Comment


    I am glad i could help

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    either24  challenge 19 73
    has77  challenge 9 58
    abstract class with all non abstract mentods 6 58
    find a node in VST 2 37
    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 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

    15 Experts available now in Live!

    Get 1:1 Help Now