tags:

views:

805

answers:

3

I've seen posts on deleting the lines from a text file that are specified as a function parameter, but I only need to delete the first and last lines from the file.

I'm still a newbie when it comes to working in files, but it seems that it should be simple to delete the first line... Just delete all text from BOF to the first CrLf character. Am I right?

As for the last line, I understand that I'll have to get a count of the lines in the text file to find it (as the file won't always be x amount of lines long). This is where I really need some help.

N.B. I'm using VB.NET 2005

+1  A: 

Files are streams, there are no shortcuts. You have to read the entire file and write it back, minus the first and last line. Horribly inefficient of course, use a database instead.

Hans Passant
You don't "have to".
dbasnett
Hmya, if only we could ignore the "delete first line" requirement.
Hans Passant
I would use a database, but the code to do this is going to be implemented in a process that strips the column headers and trailing white space from the first and last lines of a text file that the process is then going to import into the db
Logan Young
A: 

Read the file in to a list of lines as string completely. Write the file back out using an indexed loop to capture all but the first and last items.

    Dim listText As New List(Of String)
    Dim objLine As String = ""

    Using objReader As StreamReader = New StreamReader("c:\test.txt")
        Do
            objLine = objReader.ReadLine()
            If objLine IsNot Nothing Then listText.Add(objLine)
        Loop Until objLine Is Nothing
    End Using

    Using objWriter As StreamWriter = New StreamWriter("c:\testOutput.txt")
        For I As Integer = 1 To listText.Count - 2
            objWriter.WriteLine(listText.Item(I))
        Next
    End Using

Edit to satisfy the very picky:

    Dim arrText() As String
    Dim sLine As String = ""

    arrText = File.ReadAllLines("c:\test.txt")

    Using objWriter As StreamWriter = New StreamWriter("c:\testOutput.txt")
        For I As Integer = 1 To arrText.Length - 2
            objWriter.WriteLine(arrText(I))
        Next
    End Using
Joel Etherton
You may be interested in learning about the `File.ReadAllLines` method on `System.IO.File` :)
AakashM
@AakashM - Eh, it's a matter of preference really. I much prefer Lists and Collections to Arrays. ReadAllLines produces only a string array. Updated code sample to include both though.
Joel Etherton
You do not need to read all the lines to accomplish this. Wouldn't it be prudent to know the size of the file before suggesting that approach?
dbasnett
In order to remove the first line you'll need to read in the entire file. I see your case you're using byte arrays. In order to get rid of that first line you still have to read the entire file into a new byte array. Either solution is expensive for super large files. To handle super large files, you'd need to read them in stages, and write them out to a separate file in the same stage levels.
Joel Etherton
A: 

This is to delete the last line, without reading the entire file. You may need to modify the logic to account for the EOF being a newline...

    Dim fs As New FileStream("c:\test.txt", FileMode.Open, FileAccess.ReadWrite)
    Dim b(1) As Byte

    Do
        fs.Seek(fs.Length - 2, SeekOrigin.Begin) 'seek 2 bytes from EOF
        fs.Read(b, 0, 2) 'read last two bytes
        'are they newline
        If System.Text.Encoding.ASCII.GetString(b, 0, 2) <> Environment.NewLine Then
            fs.SetLength(fs.Length - 1) 'set length to -1
        Else
            fs.SetLength(fs.Length - 2)
            Exit Do
        End If
    Loop
    fs.Close()
dbasnett
All that does is remove the last line feed. It doesn't delete the first line. As far as I can see it only removes the final byte.
Joel Etherton
True. I should not have assumed that he had that under control.
dbasnett
Hi guys,Thanks for your suggestions. We can consider this case closed, see below:http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/f3619ac8-c9a5-424a-b666-93d1b78ddddf
Logan Young