I have the following function Which Is calling itself (Recursive). The goal is to return a unique filename formatted as filename (1).ext, filename (2).ext, etc.
Function CreateUniqueFileName(strPath As String, strFileName, orderId As Integer) As String
Dim extPos As Integer
Dim extension As String
Dim fileName As String
fileName = ""
extPos = InStrRev(strFileName, ".")
If (extPos > 0) Then
fileName = Left(strFileName, extPos - 1)
extension = Right(strFileName, Len(strFileName) - extPos)
If (orderId = 0) Then
fileName = strFileName
CreateUniqueFileName = fileName
Else
fileName = fileName & " (" & CStr(orderId) & ")." & extension
End If
If (DoesFileExist(strPath & fileName)) Then
Call CreateUniqueFileName(strPath, fileName, orderId + 1)
Else
CreateUniqueFileName = fileName
Exit Function
End If
End If
End Function
If it is called the first time and the orderId value is 0 this is always the first one and therefore unique. So in that case the function is only called once. But when the recursion is executed and the DoesFileExists returns false the return value should return the generated filename and exit. However, when I debug the function is executing without errors but it always returns the original value instead of the result of the original iteration.
So for example, if I call this function like this: CreateUniqueFileName("C:\Temp\",""1010-40-800.jpg",1) It checks in C:\temp if there is already a file named 1010-40-800 (1).jpg, if so the same function is called and the orderId is updated by 1 in this case CreateUniqueFileName("C:\Temp\",""1010-40-800.jpg",2). The same process is repeated (Recusive). Now assume the 1010-40-800 (2).jpg is unique (File is not found). I would expect the function to return 1010-40-800 (2).jpg as as string result. But instead it will return the value 1010-40-800 (1).jpg. Which is actually the value of the first time the function is called.
What am I missing here?