I am renaming files from .txt to .csv within a directory. the code i am using to do this is -
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:
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