D
djcamo
Hi, I'm hoping some one can help with some coding issues I'm having.
Below is some code I use to rename a file if it already exists in a
folder. As you can see I have 5 nested if loops which means that the
file can only be renamed 5 times. What I am having trouble with is a
recursive loop that allows an infinite amount of this file being
renamed. Hopes this make sense.
If newfi.Exists Then
newfilename = surname & "_1" & "_" & etick & ".txt"
Dim newfi1 As New FileInfo(agPath + newfilename)
If newfi1.Exists Then
newfilename = surname & "_2" & "_" & etick &
".txt"
Dim newfi2 As New FileInfo(agPath + newfilename)
If newfi2.Exists Then
newfilename = surname & "_3" & "_" & etick &
".txt"
Dim newfi3 As New FileInfo(agPath +
newfilename)
If newfi3.Exists Then
newfilename = surname & "_4" & "_" & etick
& ".txt"
If newfi3.Exists Then
Dim msg As String = "Unable to save
coupon " & surname & " again" & vbCr & "please delete existing record
first"
MsgBox(msg, MsgBoxStyle.Critical,
"Warning")
flag = True
Exit Sub
End If
End If
End If
End If
Else
newfilename = surname & "_" & etick & ".txt"
End If
Cheers
Dave
Below is some code I use to rename a file if it already exists in a
folder. As you can see I have 5 nested if loops which means that the
file can only be renamed 5 times. What I am having trouble with is a
recursive loop that allows an infinite amount of this file being
renamed. Hopes this make sense.
If newfi.Exists Then
newfilename = surname & "_1" & "_" & etick & ".txt"
Dim newfi1 As New FileInfo(agPath + newfilename)
If newfi1.Exists Then
newfilename = surname & "_2" & "_" & etick &
".txt"
Dim newfi2 As New FileInfo(agPath + newfilename)
If newfi2.Exists Then
newfilename = surname & "_3" & "_" & etick &
".txt"
Dim newfi3 As New FileInfo(agPath +
newfilename)
If newfi3.Exists Then
newfilename = surname & "_4" & "_" & etick
& ".txt"
If newfi3.Exists Then
Dim msg As String = "Unable to save
coupon " & surname & " again" & vbCr & "please delete existing record
first"
MsgBox(msg, MsgBoxStyle.Critical,
"Warning")
flag = True
Exit Sub
End If
End If
End If
End If
Else
newfilename = surname & "_" & etick & ".txt"
End If
Cheers
Dave