Hi
I have this function to generate a unique filename in a target folder. The function works fine, but only up to 10 unique filenames. This means that when the function have returned 10 unique filename and is supposed to make the 11'th unique name it return the 10'th instead. I use the function to create jpg files that has a unique filename.
Here is my code..
The code generate filenames like this..
myfile.jpg
myfile_1.jpg
myfile_2.jpg
myfile_3.jpg
myfile_4.jpg
myfile_5.jpg
myfile_6.jpg
myfile_7.jpg
myfile_8.jpg
myfile_9.jpg
myfile_10.jpg
When 10 filenames has been returned and the function is called again i should return myfile_11.jpg, but instead it return myfile_10.jpg. And I can't see why it behaive that way. Would very much appreciate some help here.
Best Regards
I have this function to generate a unique filename in a target folder. The function works fine, but only up to 10 unique filenames. This means that when the function have returned 10 unique filename and is supposed to make the 11'th unique name it return the 10'th instead. I use the function to create jpg files that has a unique filename.
Here is my code..
Code:
Private Function UniqueFileName(ByVal FileName As String) As String
Dim Suffix As String = String.Empty
Dim FileExtension As String
Dim FileNameWithoutExtension As String
Dim FolderPath As String
Dim i As Integer = 0
FileExtension = Path.GetExtension(FileName)
FolderPath = Path.GetDirectoryName(FileName)
FileNameWithoutExtension = Path.GetFileNameWithoutExtension(FileName)
FileNameWithoutExtension = FileNameWithoutExtension.Replace(" ", "_")
Dim DirInfo As DirectoryInfo = New DirectoryInfo(FolderPath)
For Each fi As FileInfo In DirInfo.GetFiles()
If Trim(LCase(fi.Name)) = Trim((LCase(FileNameWithoutExtension & Suffix & FileExtension))) Then
i = i + 1
Suffix = ("_" & i.ToString)
End If
Next
Return (FileNameWithoutExtension & Suffix & FileExtension)
End Function
The code generate filenames like this..
myfile.jpg
myfile_1.jpg
myfile_2.jpg
myfile_3.jpg
myfile_4.jpg
myfile_5.jpg
myfile_6.jpg
myfile_7.jpg
myfile_8.jpg
myfile_9.jpg
myfile_10.jpg
When 10 filenames has been returned and the function is called again i should return myfile_11.jpg, but instead it return myfile_10.jpg. And I can't see why it behaive that way. Would very much appreciate some help here.
Best Regards