I need to identify how the code identifies that a file is at the "eof" (end of file). Here's why:

On Monday's, I'm having to combine text files created over the weekend that will eventually be read and pulled into an Excel spreadsheet (generally there are three files). Using VB.net, I'm stripping the blank lines and the words "End of Report" from the end of the first two files before merging these together. The third file gets merged as is with no changes.

When I open the merged file (using Excel and VBA) to be read, I'm getting a error message at the beginning of the (technically) second report file (again, it's been merged with the first) that reads that it's "past the end of file". I'm trying to understand how it knows that as there is the merged data below that point. I'm trying to fix this so that it will continue to read to the last line of the report. Any thoughts?

In advance, thanks for your help.

Don

Recommended Answers

All 3 Replies

It sounds like a file pointer still pointing to the first file, while you are trying to move it further through the second file (possibly before it is appended to the first). If you are able to post code I could probably be more specific?

Here's the code that retrieves the 2 files and then strips the first file of it's last lines (that includes the phrase "End of Report"):

                    If fileCount = 2 Then
                    RestranName = getRestranName(0)
                    RestranName2 = getRestranName(1)
                    Dim readtxt() As String = File.ReadAllLines(RestranName)
                    'Deleted the actual file.
                    File.Delete(RestranName)
                    'Now time to read the array elements and save them in a file.
                    For i As Integer = readtxt.GetLowerBound(0) To readtxt.GetUpperBound(0) - 4
                        'Appending the line to the text file
                        My.Computer.FileSystem.WriteAllText(RestranName, readtxt(i), True)
                        If i < readtxt.GetUpperBound(0) - 4 Then
                            'Appending a new line into the text file.
                            My.Computer.FileSystem.WriteAllText(RestranName, vbCrLf, True)
                        End If
                    Next
                    My.Computer.FileSystem.WriteAllText(RestranName, pageCode, True)

After this is done I then merge the two files:

                    System.IO.File.AppendAllText(RestranName, System.IO.File.ReadAllText(RestranName2))
                    System.IO.File.Delete(RestranName2)
                    End If

I appreciate your ideas.

I can't see anything specifically wrong with the code you have posted. My suggestion would be to try using a StreamWriter, take a look at File.AppendText - this method opens the file once and closes it at the end, rather than open-closing for each line written. The excessive file handling operations may be causing a problem for you, and the StreamWriter will be more efficient in any case.

Also, just had the thought that the Delete of file 2 might be happening before the ReadAllText has finished handling the file. You might try removing that line to test, or adding a delay in before it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.