The above procedure adds a counter to the end, but in my case you want to save the file extension, so I expanded the function to this:
Public Shared Function FileExistIncrementer(ByVal OrginialFileName As String) As String
Dim counter As Integer = 0
Dim NewFileName As String = OrginialFileName
While File.Exists(NewFileName)
counter = counter + 1
NewFileName = String.Format("{0}\{1}-{2}{3}", Path.GetDirectoryName(OrginialFileName), Path.GetFileNameWithoutExtension(OrginialFileName), counter.ToString(), Path.GetExtension(OrginialFileName))
End While
Return NewFileName
End Function
source
share