Group,

I'm now having to merge several of these text files together. At the end of each of the text files is a line that says "End of Report". I need to eliminate this line completely.

This code does the merge for me:

                RestranName = getRestranName(0)
                RestranName2 = getRestranName(1)
                System.IO.File.AppendAllText(RestranName2, System.IO.File.ReadAllText(RestranName))

These files are UNIX created and are unformated. So I'm using the following code to format it correctly:

            ' This reads the text file for conversion
            txtLine = My.Computer.FileSystem.ReadAllText(RestranName)
            ' This begins to add the carriage returns in the appropriate places
            txtLine = Replace(txtLine, vbLf, vbCrLf)
            txtLine = Replace(txtLine, vbCr & vbCr, vbCr)
            txtLine = Replace(txtLine, """", "")
            ' This writes the line to the file
            My.Computer.FileSystem.WriteAllText(fileSave, txtLine, False)

This works perfectly. Unfortunately though I have "End of Report" between the two merged and formatted reports. I've got to figure out how to remove those words and the line (or spaces around it).

I've tried using StreamWriter to read the unformated but merged file (RestranName) to find the "End of Report" line using this code:

            Using reader As New StreamReader(RestranName)
                While Not reader.EndOfStream
                    Dim line As String = reader.ReadLine()
                    If line.Contains("End of Report") Then
                        badLine = line
                    Else
                        goodLine = line
                        My.Computer.FileSystem.WriteAllText(RestranName2, goodLine, True)
                    End If
                End While
            End Using

This works to remove the line that includes "End of Report". Unfortunately when it goes through the routine for formatting, it doesn't do it correctly. Any thoughts as to why? Any idea how to fix?

Thanks,

Don

This is a procedural problem. Think about how you would solve the conundrum. There are a couple of options:

  1. Before appending a file to the data set, forward to the last line and remove it.
  2. After appending a file to the data set, forward to the last line and remove it.
  3. Append each line of a source file to the destination file, and when you find the "End of Report" line, don't write it to the destination file.

Are we seeing a pattern here? To do this, you need to open the file for read+write options, not append-only. It isn't difficult, and definitely not rocket science.

Hi Don,
I think the only thing you need to do is changing:
My.Computer.FileSystem.WriteAllText(RestranName2, goodLine, True) to:
My.Computer.FileSystem.WriteAllText(RestranName2, goodLine & vbCrLf , True)
adding a carrige return and linefeed

You use here the contains method to detect the end line of the file. But it can remove all lines which contain the keyword "End of Report".

For example:
suppose there is a line like "The rules and regulations are described at the End of Report" in the middle of the report.
The contains method will straightly detect it and as per your codes this line would be removed from the middle of your report.

so compair the contents in lieu of using contains method to find that keyword.
the codes should be

 Using reader As New StreamReader(RestranName)
    While Not reader.EndOfStream
        Dim line As String = reader.ReadLine()
        If Not (line="End of Report") Then
            goodLine = line
            My.Computer.FileSystem.WriteAllText(RestranName2, goodLine & VbCrlf, True)
        End If
    End While
 End Using

Hope it can help you.

Rubberman, I think your idea is probably a wise one. I'm disappointed I didn't think of it first. I guess I'm in too big a hurry to get this thing working right.

Minimalist, I'll try that one as well.

You might as well replace

goodLine = line
My.Computer.FileSystem.WriteAllText(RestranName2, goodLine & VbCrlf, True)

with

My.Computer.FileSystem.WriteAllText(RestranName2, Line & VbCrlf, True)

It's pointless to declare another temporary variable.

There is a part two to my issue:

There is a blank line between the end of the last line of info and the line that contains "End of Report". I also need to delete this 1 line. My challenge is that there is a blank line between each row of data that I cannot delete.

So the question is: Is there some code that would delete not only the line containing "End of Report" but also the line before?

As always, Thanks.

Don

Edited 1 Year Ago by doncwilson_1

I did do this to remove the "End of Report":

txtLine = Replace(txtLine, "End of Report", "")

Of course it still leaves the line. I'll play with this to see if I can't remove the whole line.

I'm curious: Using the "System.IO.File.ReadAllLines("FileName")" syntax, is there a way to read a specific number of lines?

I've determined there are 232 lines in the first file. If I read only 230 of them, I've eliminated the last blank line and the line that contains "End of Report". Is this possible?

Read all lines and go through a loop.
The codes should be

Dim readtxt() As String = File.ReadAllLines(RestranName)

For i As Integer = readtxt.GetLowerBound(0) To readtxt.GetUpperBound(0) - 2
    My.Computer.FileSystem.WriteAllText(RestranName2, readtxt(i) & vbCrLf, True)
Next

Hope it can help you.

Shark 1, no luck using your code. It doesn't appear to be doing anything. I was hoping it would drop the last two lines. But it doesn't seem to be doing it.

This prompts me to ask again, should you use StreamReader to loop through this? Since I know there are 232 lines, could I tell it to loop through StreamReader 230 times? If so, I can't seem to find the correct syntax to tell it to exit the "Using" early. Thoughts or ideas?

Edited 1 Year Ago by doncwilson_1

I do not know, how did you use the above codes.
In a button click event you can use it.

Me.Cursor = Cursors.WaitCursor

        'Declaring variable to store the file path & name
        Dim RestranName As String = "C:\My XXFiles\Text Document.txt"

        'Declare an array
        'reading entire file content and store them into the array line by line.
        Dim readtxt() As String = File.ReadAllLines("C:\My XXFiles\Text Document.txt")

        'Deleted the actual file.
        File.Delete("C:\My XXFiles\Text Document.txt")

        'Now time to read the array elements and save them in a file.
        For i As Integer = readtxt.GetLowerBound(0) To readtxt.GetUpperBound(0) - 2

            'Appending the line to the text file
            My.Computer.FileSystem.WriteAllText(RestranName, readtxt(i), True)

            If i < readtxt.GetUpperBound(0) - 2 Then
                'Appending a new line into the text file.
                My.Computer.FileSystem.WriteAllText(RestranName, vbCrLf, True)
            End If
        Next
        Me.Cursor = Cursors.Default

This is a simple codification. open a text file, read & store it in an array line by line, save the lines what you desire. Done.
Please, Change the path and name of file.
Try it. It is working in my computer.

Edited 1 Year Ago by Shark_1

Shark 1, I got it to work. I did find that the code readtxt.GetUpperBound(0) - 2 had to be changed to -4. Once I did that it worked exactly as I needed it. Thank you!

This question has already been answered. Start a new discussion instead.