Error handling question

Camus

Active member
Joined
Oct 4, 2007
Messages
28
Programming Experience
Beginner
I am renaming files from .txt to .csv within a directory. the code i am using to do this is -


VB.NET:
        Dim number As Integer

        number = 5


        Do While number = 5


                Dim dir As DirectoryInfo = New DirectoryInfo("\\IFADATAFLOW1\SharedDocs\Test production bucket folder")

                For Each fileinfo As IO.FileInfo In dir.GetFiles
                    If fileinfo.Extension.ToLower = ".txt" Then
                        Dim sb As New StringBuilder
                        sb.Append(Path.GetDirectoryName(fileinfo.FullName))
                        sb.Append("\")
                        sb.Append(Path.GetFileNameWithoutExtension(fileinfo.FullName))
                        sb.Append(".csv")
                        fileinfo.MoveTo(sb.ToString)
                    End If
                Next

As you can see I have created an endless loop here because i want the code to be constantly running so that files are renamed as soon as they are saved to the directory.

This works fine 85% of the time but occasionally an error occurs and my program exits, i think this is because it is trying to rename a txt file which is still in use by the program that is creating it.

To get around this i used a Try...Catch statement and repeated the above code within 'catch'...so it is effectively having two attempts at renaming the file. This improved the success rate but it errors still occur.

What would be alot better is if i could say 'on error, restart the loop', is there any way to do this?


by the way, this is what my code looks like at the moment using the try...catch statements...


VB.NET:
Imports system.io
Imports System.Text


Public Class Form1



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim number As Integer

        number = 5



        Try

            Do While number = 5


                Dim dir As DirectoryInfo = New DirectoryInfo("\\IFADATAFLOW1\SharedDocs\Test production bucket folder")

                For Each fileinfo As IO.FileInfo In dir.GetFiles
                    If fileinfo.Extension.ToLower = ".txt" Then
                        Dim sb As New StringBuilder
                        sb.Append(Path.GetDirectoryName(fileinfo.FullName))
                        sb.Append("\")
                        sb.Append(Path.GetFileNameWithoutExtension(fileinfo.FullName))
                        sb.Append(".csv")
                        fileinfo.MoveTo(sb.ToString)
                    End If
                Next


            Loop

        Catch

            Try

                Do While number = 5

                    Dim dir As DirectoryInfo = New DirectoryInfo("\\IFADATAFLOW1\SharedDocs\Test production bucket folder")

                    For Each fileinfo As IO.FileInfo In dir.GetFiles
                        If fileinfo.Extension.ToLower = ".txt" Then
                            Dim sb As New StringBuilder
                            sb.Append(Path.GetDirectoryName(fileinfo.FullName))
                            sb.Append("\")
                            sb.Append(Path.GetFileNameWithoutExtension(fileinfo.FullName))
                            sb.Append(".csv")
                            fileinfo.MoveTo(sb.ToString)

                        End If
                    Next
                Loop
            Catch

                Do While number = 5

                    Dim dir As DirectoryInfo = New DirectoryInfo("\\IFADATAFLOW1\SharedDocs\Test production bucket folder")

                    For Each fileinfo As IO.FileInfo In dir.GetFiles
                        If fileinfo.Extension.ToLower = ".txt" Then
                            Dim sb As New StringBuilder
                            sb.Append(Path.GetDirectoryName(fileinfo.FullName))
                            sb.Append("\")
                            sb.Append(Path.GetFileNameWithoutExtension(fileinfo.FullName))
                            sb.Append(".csv")
                            fileinfo.MoveTo(sb.ToString)

                        End If
                    Next
                Loop



            End Try

        End Try



   

    End Sub
 
hey thanks...so calling the function from a loop will cause it to keep repeating the loop when an error occurs?


im not completely sure how to create the function or call it from a loop, any tips?
 
Thanks, I did that. By the way, I got around the error handling problem by using 'on error resume next' so I did away with the try catch statements.
 
Thanks, I did that. By the way, I got around the error handling problem by using 'on error resume next' so I did away with the try catch statements.
That's a step backwards. You should be moving from On Error Resume Next to Try...Catch. If you want a loop to continue after an exception then the loop should not be inside the Try block. The whole Try...Catch block should be inside the loop, e.g.
VB.NET:
Do
    Try
        'Do something here.
    Catch
        'Clean up if required.
    End Try
Loop
 
You also have the Path.ChangeExtension Method and FileSystemWatcher class (in Toolbox, Components too).
 
Back
Top