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

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.

Hi

When you meet your condition, simply close the reader then use the File.Delete method to delete the file before exiting your loop.

HTH

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 1 Year Ago by doncwilson_1

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