VB.net - How to take a text file and delete the CRLFs in the file

Hello!

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.  VB.net 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!
LVL 1
gderoeckAsked:
Who is Participating?
 
rsmithntCommented:
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
                    tmp_writer.WriteLine(tmp_reader.ReadLine())
                Loop

                '-------------------------------------------------
                '   Cleans up Variables
                '-------------------------------------------------
                FilePath = Nothing
                tmp_filename = Nothing
                Filename = Nothing
               
                tmp_reader.Close()
                tmp_reader = Nothing

                tmp_writer.Close()
                tmp_writer = Nothing
            End Sub
0
 
rsmithntCommented:
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.
0
 
gderoeckAuthor Commented:
Can you post some code to show this?  I'm trying to do what you suggested, but not having much luck.
0
 
gderoeckAuthor Commented:
Excellent!  Thank you!
0
 
rsmithntCommented:
Anytime.

I am glad i could help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.