0

Group,

I'm using the following code to read down to the third line of a text file. If it meets the criteria, I need to stop the read and then delete the file. However I'm finding it difficult to do this as everything I've tried has failed. Have you some thoughts on how I can do this?

                        Do While objReader.Peek() <> -1
                            txtLine = objReader.ReadLine()
                            programName = Trim(Microsoft.VisualBasic.Left(txtLine, 14))

                            If lineNo = 3 And programName = "(avl.allover)" Then
                                fileSave = hotelFolder & "\Daily Reports\" & propertyNo & "Allover.txt"
                                System.IO.File.Copy(fileName, fileSave, True)
                                ' Stop the reading of the file and delete the file now.  But how?
                                Exit Do

                            End If

In advance, thanks for your help.

Don

3
Contributors
3
Replies
14
Views
2 Years
Discussion Span
Last Post by doncwilson_1
1

You can't (easily) delete the file while it's locked for reading, so I'd alter the logic so that you break out of the reading loop, destroy the reader object, then delete the file safely.

0

djjeavons, I specifically used your idea as I have multiple files in the folder, some of which will be deleted and some that won't. So here's what I did:

                    If System.IO.File.Exists(fileName) Then
                        Do While objReader.Peek() <> -1
                            txtLine = objReader.ReadLine()
                            programName = Trim(Microsoft.VisualBasic.Left(txtLine, 14))

                            If lineNo = 3 And programName = "(avl.allover)" Then
                                fileSave = hotelFolder & "\Daily Reports\" & propertyNo & "Allover.txt"
                                System.IO.File.Copy(fileName, fileSave, True)
                                objReader.Dispose()
                                System.IO.File.Delete(fileName)
                            End If

                            If lineNo = 3 And programName = "(avl.dasrpt)" Then
                                fileSave = hotelFolder & "\Forecast\14 Day Forecast\" & propertyNo & "dasrpt.txt"
                                System.IO.File.Copy(fileName, fileSave, True)
                                objReader.Dispose()
                                System.IO.File.Delete(fileName)
                            End If

                            lineNo = lineNo + 1
                            If lineNo > 3 Then
                                lineNo = 1
                                Exit Do
                            End If
                        Loop

                    End If

I decided to "dispose" of the objectReader. This seamed to work better as I was to continue looking through the folder for other files.

As always, thanks for the help. The group here is the best!

Don

Edited by doncwilson_1

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.