Connecting Tech Pros Worldwide Help | Site Map

help renaming file

djcamo@internode.on.net
Guest
 
Posts: n/a
#1: Aug 27 '08
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
=?Utf-8?B?U3VydHVyWg==?=
Guest
 
Posts: n/a
#2: Aug 27 '08

re: help renaming file


This code will do what you want:

Function CreateFile(ByVal agPath As String, ByVal surname As String,
ByVal etick As String) As String
Dim strFilename As String = ""
Dim i As Integer = 1
Do
strFilename = surname & "_" & i.ToString & "_" & etick & ".txt"
Dim fnfNew As New FileInfo(agPath & strFilename)
If Not fnfNew.Exists Then
Exit Do
End If
i += 1
Loop
Return strFilename
End Function

However, it has two problems:
1) You really should limit the number of retries as you could end up hanging
the system
2) You really need to create the file as part of checking if the same
program will be run multiple times simultaneously.


--
David Streeter
Synchrotech Software
Sydney Australia


"djcamo@internode.on.net" wrote:
Quote:
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
>
djcamo@internode.on.net
Guest
 
Posts: n/a
#3: Aug 29 '08

re: help renaming file


On Aug 27, 4:22*pm, SurturZ <surt...@newsgroup.nospamwrote:
Quote:
This code will do what you want:
>
* * Function CreateFile(ByVal agPath As String, ByVal surname As String,
ByVal etick As String) As String
* * * * Dim strFilename As String = ""
* * * * Dim i As Integer = 1
* * * * Do
* * * * * * strFilename = surname & "_" & i.ToString & "_" & etick & ".txt"
* * * * * * Dim fnfNew As New FileInfo(agPath & strFilename)
* * * * * * If Not fnfNew.Exists Then
* * * * * * * * Exit Do
* * * * * * End If
* * * * * * i += 1
* * * * Loop
* * * * Return strFilename
* * End Function
>
However, it has two problems:
1) You really should limit the number of retries as you could end up hanging
the system
2) You really need to create the file as part of checking if the same
program will be run multiple times simultaneously.
>
--
David Streeter
Synchrotech Software
Sydney Australia
>
>
>
"djc...@internode.on.net" wrote:
Quote:
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.
>
Quote:
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 msgAs 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
>
Quote:
Cheers
Dave- Hide quoted text -
>
- Show quoted text -
thanks
Closed Thread