Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

make unique filenames function stop at file number 10

Status
Not open for further replies.

lin73

Programmer
Feb 17, 2006
110
SE
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..

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
 
Hello,

This code worked for me. Good Luck!
Code:
Dim DirInfo As DirectoryInfo = _
    New DirectoryInfo(FolderPath)

Dim BaseFileName As String = _
    FileNameWithoutExtension.Replace("_"c, String.Empty)

i = DirInfo.GetFiles(BaseFileName & "*").Length
Suffix = ("_" & i.ToString)

With New FileInfo( _
    FolderPath & "\" & BaseFileName & _
        Suffix & FileExtension)
            If Not .Exists Then .Create()
End With
Return (BaseFileName & Suffix & FileExtension)


 
Hi

Thanks!
The code itself works just fine, but would you mind explaining what the ("_"c and BaseFileName & "*" part does?

Best Regards
 
Your initial code doesn't work because the numbers are sorted as characters, so once you are at 10, the sort becomes:

1
10
2
...

and you miss the compare on the 10.

The "_"c makes the _ a character rather than a string.

The "*" gets all files starting with the base name - the Length property returns the number of files that meet the mask criteria.

"I think we're all Bozos on this bus!" - Firesign Theatre [jester]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top