PDA

View Full Version : Incremental number for saved files



mdmackillop
04-03-2008, 03:17 AM
I use the following code to add incremental numbers to saved files to avoid duplication/overwriting. As FileSearch is not supported in 2007, what is the most efficient way to do this?


'Check if the attachment name exists, if so, add an increment
Function DocSave(StrFolderPath As String, DocName As String, Ext As String) As String
Dim Ext As String, SaveName As String
Dim fs As FileSearch
Set fs = Application.FileSearch
With fs
.NewSearch
.LookIn = StrFolderPath
.SearchSubFolders = False
.Filename = DocName
.MatchTextExactly = False
.FileType = msoFileTypeAllFiles
If .Execute() > 0 Then
DocSave = DocName & "-" & .FoundFiles.Count & Ext
Increment = True 'to show "Saved As" message
Else
DocSave = DocName & Ext
End If
End With
Set fs = Nothing
End Function

Charlize
04-03-2008, 03:41 AM
Most efficient I don't know, but it's doing something.Sub testit()
Dim newname As String
newname = Charlizes_Dir("C:\Temp", "Data", ".Doc")
MsgBox newname
End Sub
Function Charlizes_Dir(StrFolderPath As String, DocName As String, Ext As String) As String
Dim myfilename As String
Dim mycount As Long
mycount = 1
myfilename = Dir(StrFolderPath & "\" & DocName & "*")
Do While myfilename <> vbNullString
mycount = mycount + 1
myfilename = Dir
Loop
Charlizes_Dir = DocName & "-" & mycount & Ext
End Function

tstav
04-03-2008, 05:32 AM
Suppose the name I want to increment is "Data.xls" but there are also other files named e.g. "DataMining.xls". It seems Charlize's option would give "Data-2.xls" when it should be "Data-1.xls".

Sub CreateNewFileName()
'----------------------------------------------------
'This version builds a suffix always one greater than
'the already existing max suffix.
'If no suffix exists, then suffix=1
'----------------------------------------------------
Dim newFileName As String, strPath As String
Dim strFileName As String, strExt As String
strPath = "C:\VBA_tstav"
strFileName = "VBA_Forums"
strExt = ".xls"
newFileName = strFileName & "-" & GetNewSuffix(strPath, strFileName, strExt) & strExt
MsgBox newFileName
End Sub

Function GetNewSuffix(ByVal strPath As String, ByVal strName As String, ByVal strExt As String)
Dim strFile As String, strSuffix As String, intMax As Integer
On Error Resume Next
strFile = Dir(strPath & "\" & strName & "*")
Do While strFile <> ""
strSuffix = Mid(strFile, Len(strName) + 1, Len(strFile) - Len(strName) - Len(strExt))
If Len(strSuffix) > 0 Then 'strName has suffix
If Left(strSuffix, 1) = "-" And CInt(Right(strSuffix, Len(strSuffix) - 1)) >= 0 Then
If Err Then
'ignore this file (it starts with strName but the fullname is different)
Err.Clear
Else
If CInt(Right(strSuffix, Len(strSuffix) - 1)) >= intMax Then
intMax = CInt(Right(strSuffix, Len(strSuffix) - 1))
End If
End If
End If
End If
strFile = Dir
Loop
GetNewSuffix = intMax + 1
End Function

Charlize
04-03-2008, 05:46 AM
:doh:Aha, try this one.Sub testit()
Dim newname As String
newname = Charlizes_Dir("C:\Temp", "Data", ".Doc")
MsgBox newname
End Sub
Function Charlizes_Dir(StrFolderPath As String, DocName As String, Ext As String) As String
Dim myfilename As String
Dim mycount As Long
mycount = 1
myfilename = Dir(StrFolderPath & "\" & DocName & "*")
Do While myfilename <> vbNullString
If Len(StrFolderPath & "\" & Left(myfilename, Len(myfilename) - 4)) = _
Len(StrFolderPath & "\" & DocName) Then
mycount = mycount + 1
End If
myfilename = Dir
Loop
Charlizes_Dir = DocName & "-" & mycount & Ext
End Function

mdmackillop
04-03-2008, 05:51 AM
My names will be created by code. I agree Data and DataMining could both cause an increment. This is unlikely and in any case would not matter . I'm more concerned with overwriting than getting consecutive numbers.
On the other hand, I can see that numbering may be of use to others, so let's include it in the problem!

Charlize
04-03-2008, 06:36 AM
:yay Sub testit()
Dim newname As String
newname = Charlizes_Dir("C:\Temp", "Data", ".Doc")
MsgBox newname
End Sub
Function Charlizes_Dir(StrFolderPath As String, DocName As String, Ext As String) As String
Dim myfilename As String
Dim mycount As Long
mycount = 1
myfilename = Dir(StrFolderPath & "\" & DocName & "*")
Do While myfilename <> vbNullString
'if no - are used inside the filename you can use split
If Len(StrFolderPath & "\" & Split(myfilename, "-")(0)) = _
Len(StrFolderPath & "\" & DocName) Or _
Len(StrFolderPath & "\" & myfilename) = _
Len(StrFolderPath & "\" & DocName & Ext) Then
mycount = mycount + 1
End If
myfilename = Dir
Loop
If mycount > 1 Then
Charlizes_Dir = DocName & "-" & mycount & Ext
Else
Charlizes_Dir = DocName & Ext
End If
End Function

tstav
04-03-2008, 07:58 AM
Charlize, try it with the filenames
"Data.doc", "Data-2.doc", "Data-4.doc"
or "Data-2.doc", "Data-3.doc", "Data-4.doc"
or... (more possibilities)...
It would produce the name "Data-4.doc" and it would overwrite the already existing one.

What I mean to say is that we not only have to count the "matching" files, but also keep track of the max suffix in order to add one to it and eliminate any chance of overwriting (post #3).

Charlize
04-03-2008, 10:21 AM
Charlize, try it with the filenames
"Data.doc", "Data-2.doc", "Data-4.doc"
or "Data-2.doc", "Data-3.doc", "Data-4.doc"
or... (more possibilities)...
It would produce the name "Data-4.doc" and it would overwrite the already existing one.

What I mean to say is that we not only have to count the "matching" files, but also keep track of the max suffix in order to add one to it and eliminate any chance of overwriting (post #3).Probably. But I assume that when you use automatic saving, that wouldn't be the case, would it ?

If you save by using the function, it would give the first name data, the second would be data-2, even with datamining in the folder. So not data-3 but indeed data-2 when you save a data the second time. (post #6)

Charlize

ps.: Nevertheless, it's an interesting approach.

mdmackillop
04-03-2008, 10:29 AM
I'll test them shortly
Thanks to both.

Charlize
04-03-2008, 10:33 AM
I'll test them shortly
Thanks to both.Your welcome. But one other glitch is the file extensions. For the new Office 2007 it's 4 characters. Maybe should I check on the extension length to ?

Charlize

tstav
04-03-2008, 10:45 AM
But I assume that when you use automatic saving, that wouldn't be the case, would it ?

You're right Charlize. Automatic saving creates no problems in the beginning. But with time users tend to delete files. And they will. And then problems may come up.

Example 1 (I've seen it happen) is the one I mentioned earlier (post #7). They deleted "Data.doc" and "Data-1.doc" and were left with -2,-3,-4 on their disk. The program overwrote file "Data-4.doc".

Example 2 would be to delete all files but Data-4.doc. After that, the code would save as "Data-2.doc" which might create confusion as to which file was saved last, the -2.doc or the -4.doc?

That's why I always increment the suffix number to one greater than the maximum found on disk.

tstav

Edit: For the ext length see also Post #3, though I don't see any problems in your post #6 either.

tstav
04-03-2008, 12:10 PM
This seems to be a bit "cleaner" I think.
Sub CreateNewFileName()
'----------------------------------------------------
'This version builds a suffix always one greater than
'the already existing max suffix.
'----------------------------------------------------
Dim newFileName As String, strPath As String
Dim strFileName As String, strExt As String
strPath = "C:\VBA_tstav"
strFileName = "Data"
strExt = ".xls"
newFileName = strFileName & "-" & GetNewSuffix(strPath, strFileName, strExt) & strExt
MsgBox newFileName
End Sub

Function GetNewSuffix(ByVal strPath As String, ByVal strName As String, ByVal strExt As String) as integer
Dim strFile As String
Dim intMax As Integer, intSuffix As Integer, i As Integer
On Error Resume Next
strFile = Dir(strPath & "\" & strName & "*")
Do While strFile <> ""
'Exclude the extension
strFile = Left(strFile, Len(strFile) - Len(strExt))
If Len(strFile) > Len(strName) Then
'Get location of "-"
i = InStrRev(strFile, "-")
'Get the suffix number
intSuffix = IIf(i > 0, CInt(Mid(strFile, i + 1)), 0)
If Err Then
Err.Clear
Else
'Update the Max suffix number
intMax = IIf(intSuffix >= intMax, intSuffix, intMax)
End If
End If
strFile = Dir
Loop
'This is the incremented suffix number
GetNewSuffix = intMax + 1
End Function

Simon Lloyd
04-03-2008, 12:21 PM
Malcom i know you know (as you have proven) but i may be missing the point, you said...


I'm more concerned with overwriting than getting consecutive numbers.

i would have thought the simplest solution would be to save as:

ThisWorkbook.SaveAs (ThisWorkbook.Name & " " & Format(Now, "dd-mm-yy hh.mm.ss"))

never get an overwrite again unless you are saving more than one book a second!

tstav
04-03-2008, 12:26 PM
Apologies.
My post #12 won't work correctly in rare occasions like if a file named "Data-Whatever-1.doc" exists (with two dashes), because it'll count it as a valid file.

I will have to stick to post #3.

mdmackillop
04-03-2008, 01:36 PM
Hi Tstav,
Making this change and it seems perfect :thumb

strFile = Dir(strPath & "\" & strName & "-" & "*")

and a slight adjustment to your calling sub for easier testing

Sub CreateNewFileName()
'----------------------------------------------------
'This version builds a suffix always one greater than
'the already existing max suffix.
'----------------------------------------------------
Dim newFileName As String, strPath As String
Dim strFileName As String, strExt As String
strPath = "C:\AAA"
strFileName = "Data-fil"
strExt = ".xls"
newFileName = strFileName & "-" & GetNewSuffix(strPath, strFileName, strExt) & strExt
ActiveWorkbook.SaveCopyAs newFileName
MsgBox newFileName
End Sub

mdmackillop
04-03-2008, 02:00 PM
Charlize,
Nearly there but not quite, and I hadn't thought of the deleted files scenario either.

Simon,
I considered the Time route but it gets clumsy IMO. I would cetainly use it for archiving, where the date is important, but not necessary for my purpose.

Tstav
I think this should go in as a KB item.

Thanks all
Malcolm

Simon Lloyd
04-03-2008, 02:34 PM
Ok Malcolm, how about this one?

Dim myNum, wn As String
wn = ActiveWorkbook.Name
myNum = Val(Mid$(wn, InStrRev(wn, "-") + 1))
If myNum = Year(Date) Then
myNum = 1
Else
myNum = myNum + 1
End If
sPath = ThisWorkbook.Path
saveName = sPath & ThisWorkbook.Name & " " & Format((Now), "dd-mmm-yyyy") & " Rev." & myNum & ".xls"
ActiveWorkbook.SaveAs (saveName)
i use this to have sameday saved files but without the time, so i just get a version change!

Zack Barresse
04-03-2008, 02:44 PM
Tstav
I think this should go in as a KB item.
I would have to agree here. I would also say it would be preferrable to take care of the multiple "-" characters first, but it is very nice as it stands. :yes

Zack Barresse
04-03-2008, 04:54 PM
I use the following code to add incremental numbers to saved files to avoid duplication/overwriting. As FileSearch is not supported in 2007, what is the most efficient way to do this?
Something like this I would say that a naming convention is the most important thing you could use. Doing so would allow you to loop through your files with either the FSO method or the Dir() method, basically the only two methods available (to answer your original question). I would think about looping through and parsing the values either into a range to sort with, or array. My 2 cents.

tstav
04-04-2008, 12:43 AM
Good day everybody!
Sorry for the late answer, I had to go away.
Malcolm, thank you for proposing that the code should go in as a KB entry and Zack, thank you for agreeing.
Taking your suggestions and thoughts into consideration, I gave the code a revamp and included the following:
A. Include the "-" in the Dir() function.
B. More elaborate check of the validity of the suffix
1. to contain no alphanumeric characters
2. not to contain a decimal point
C. The fileName should not end with a "-" and have no extention (there could be a "Data-" file on disk, users are unpredictable).

Here's the new code for your suggestions and feed back. Thank you all.
Sub CreateNewFileName()
'----------------------------------------------------
'This version builds a suffix always one greater than
'the already existing max suffix.
'----------------------------------------------------
Dim newFileName As String, strPath As String
Dim strFileName As String, strExt As String
strPath = "C:\"
strFileName = "Data"
strExt = ".xls"
newFileName = strFileName & "-" & GetNewSuffix(strPath, strFileName, strExt) & strExt
ActiveWorkbook.SaveCopyAs newFileName
MsgBox newFileName
End Sub

Function GetNewSuffix(ByVal strPath As String, ByVal strName As String, ByVal strExt As String) as Integer
Dim strFile As String, strSuffix As String, intMax As Integer
On Error Resume Next
strFile = Dir(strPath & "\" & strName & "-" & "*")
Do While strFile <> ""
strSuffix = Mid(strFile, Len(strName) + 2, Len(strFile) - Len(strName) - Len(strExt) - 1)
If Err Then
'This file ends with a "-" and has no extention => skip it
Err.Clear
Else
'In the next line, enter "." if the decimal point of your regional settings is ","
'or enter "," if the decimal point of your regional settings is "."
If Mid(strFile, Len(strName) + 1, 1) = "-" And CSng(strSuffix) >= 0 And InStr(1, strSuffix, ".") = 0 Then
If Err Then
Err.Clear 'This file has an invalid suffix => skip it
ElseIf CSng(strSuffix) > CInt(CSng(strSuffix)) Then 'Decimal point found in suffix => skip this file
Else
If CInt(strSuffix) >= intMax Then
intMax = CInt(strSuffix)
End If
End If
End If
End If
strFile = Dir
Loop
GetNewSuffix = intMax + 1
End Function

mdmackillop
04-04-2008, 12:49 AM
As usual, idiot proofing the code is tougher than the simple functionality required!

Charlize
04-04-2008, 05:15 AM
Maybe we should trim or replace the spaces in the filename first. ie.
Data - and Data- are the same for us but not for the coding.

Charlize

mdmackillop
04-04-2008, 05:18 AM
I would probably split the function in two parts
1. Check for a valid name
2. Save with an increment
That way I could omit step 1 for code created names.

tstav
04-04-2008, 10:22 AM
Charlize, I noticed your recommendation and I agree that this could also be sth to cater for.
So, I'm sending two versions.
One checks for "-".
The other checks for either "-" or " -".
Sub CreateNewFileName()
'-----------------------------------------------------------------------
'Builds a suffix always one greater than the already existing max suffix
'-----------------------------------------------------------------------
Dim newFileName As String, strPath As String
Dim strFileName As String, strExt As String
strPath = "C:\"
strFileName = "Data"
strExt = ".xls"
newFileName = strFileName & "-" & GetNewSuffix1(strPath, strFileName, strExt) & strExt
ActiveWorkbook.SaveCopyAs newFileName
MsgBox newFileName
End Sub

Function GetNewSuffix1(ByVal strPath As String, ByVal strName As String, ByVal strExt As String) As Integer
'------------------------------
'Checks files "Name-suffix.ext"
'------------------------------
Dim strFile As String, strSuffix As String, intMax As Integer
On Error GoTo ErrorHandler
strFile = Dir(strPath & "\" & strName & "-" & "*")
Do While strFile <> ""
strSuffix = Mid(strFile, Len(strName) + 2, Len(strFile) - Len(strName) - Len(strExt) - 1)
'In the next line, enter "." if decimal point in regional settings is ","
'or enter "," if decimal point in regional settings is "."
If Mid(strFile, Len(strName) + 1, 1) = "-" And CSng(strSuffix) >= 0 And InStr(1, strSuffix, ".") = 0 Then
If CSng(strSuffix) = CInt(CSng(strSuffix)) Then 'No decimal point in suffix => valid file
If CInt(strSuffix) >= intMax Then intMax = CInt(strSuffix)
End If
End If
NextFile:
strFile = Dir
Loop
GetNewSuffix1 = intMax + 1
Exit Function

ErrorHandler:
If Err Then
Err.Clear
Resume NextFile
End If
End Function

and this one is after Charlize's recommendation:
Function GetNewSuffix2(ByVal strPath As String, ByVal strName As String, ByVal strExt As String) As Integer
'-----------------------------------------------------------
'Checks files "Name-suffix.ext" AND files "Name -suffix.ext"
'-----------------------------------------------------------
Dim strFile As String, strSuffix As String, intMax As Integer
On Error GoTo ErrorHandler
strFile = Dir(strPath & "\" & strName & "*")
Do While strFile <> ""
'Cater for " -". If no "-", skip file, otherwise get the suffix string.
If Mid(strFile, Len(strName) + 1, 2) = " -" Then
strSuffix = Mid(strFile, Len(strName) + 3, Len(strFile) - Len(strName) - Len(strExt) - 2)
ElseIf Mid(strFile, Len(strName) + 1, 1) = "-" Then
strSuffix = Mid(strFile, Len(strName) + 2, Len(strFile) - Len(strName) - Len(strExt) - 1)
Else
GoTo NextFile 'No "-" found => skip file
End If
'In the next line, enter "." if the decimal point of your regional settings is ","
'or enter "," if the decimal point of your regional settings is "."
If CSng(strSuffix) >= 0 And InStr(1, strSuffix, ".") = 0 Then
If CSng(strSuffix) = CInt(CSng(strSuffix)) Then ' No decimal point in suffix => valid file
If CInt(strSuffix) >= intMax Then intMax = CInt(strSuffix)
End If
End If
NextFile:
strFile = Dir
Loop
GetNewSuffix2 = intMax + 1
Exit Function

ErrorHandler:
If Err Then
Err.Clear
Resume NextFile
End If
End Function

rory
04-04-2008, 05:41 PM
Can't you just adjust Charlize's function to this:
Function Charlizes_Dir(StrFolderPath As String, DocName As String, Ext As String) As String
Dim myfilename As String
Dim mycount As Long
myfilename = Dir(StrFolderPath & "\" & DocName & Ext)
If myfilename = "" Then
Charlizes_Dir = DocName & Ext
Else
mycount = 0
Do
mycount = mycount + 1
myfilename = Dir(StrFolderPath & "\" & DocName & "-" & mycount & Ext)
Loop While myfilename <> vbNullString
Charlizes_Dir = DocName & "-" & mycount & Ext
End If
End Function
not overly tested so I may have missed something!

tstav
04-04-2008, 10:58 PM
rory, post#25 code will surely save a valid new filename.
But other files may already exist on disk with increments greater than the one just created. And that would create confusion as to which file was saved last.
We tend to expect the last saved file to have the greatest increment, don't we? See Post#11.

To sum up:
If the question is "Give me a filename with a dash plus a unique number", then yes, post#25 code is very elegant and absolutely sufficient.

Practice has shown me though, that the above opens the door to future problems and hassles.

rory
04-07-2008, 07:30 AM
How about this version?
Function Get_Filename(StrFolderPath As String, DocName As String, Ext As String) As String
Dim myfilename As String, strPattern As String, strReplace As String
Dim lngNewNum As Long, lngMaxNum As Long
Dim fso As Object
Dim fdr As Object, fls As Object, fil As Object
Dim regex As Object, objMatchCol As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(StrFolderPath & "\" & DocName & Ext) Then
Set fls = fso.GetFolder(StrFolderPath).Files
strPattern = "^" & DocName & " ?- ?\d+" & Ext & "$"
strReplace = "^" & DocName & " ?- ?"
Set regex = CreateObject("vbscript.regexp")
With regex
.Global = True
For Each fil In fls
.Pattern = strPattern
Set objMatchCol = .Execute(fil.Name)
If objMatchCol.Count Then
.Pattern = strReplace
lngNewNum = Val(.Replace(fil.Name, ""))
If lngNewNum > lngMaxNum Then lngMaxNum = lngNewNum
End If
Next fil
End With
Get_Filename = DocName & "-" & lngMaxNum + 1 & Ext
Else
Get_Filename = DocName & Ext
End If
End Function


again not tested exhaustively.

tstav
04-07-2008, 03:20 PM
Thanks for the alternative rory. I haven't tested it yet, but as soon as I do I will let you know.

I do feel though, that for most people the following should be easier to follow, let alone customize to whatever they wish. Not many are familiar with all those objects, their properties and methods.

Thank you again for showing interest in this.
Best regards, tstav
Sub CreateNewFileName()
Dim newFileName As String, strPath As String
Dim strFileName As String, strExt As String
strPath = "C:\" 'Change to suit
strFileName = "Data" 'Change to suit
strExt = ".xls" 'Change to suit
newFileName = strFileName & "-" & GetNewSuffix(strPath, strFileName, strExt) & strExt
MsgBox "The new FileName is: " & newFileName
End Sub

Function GetNewSuffix(ByVal strPath As String, ByVal strName As String, ByVal strExt As String) As Integer
Dim strFile As String, strSuffix As String, intMax As Integer
On Error GoTo ErrorHandler
'File's name
strFile = Dir(strPath & "\" & strName & "*")
Do While strFile <> ""
'File's suffix starts 2 chars after 'root' name (right after the "-")
strSuffix = Mid(strFile, Len(strName) + 2, Len(strFile) - Len(strName) - Len(strExt) - 1)
'FileName is valid if 1st char after name is "-" and suffix is numeric with no dec point
'Skip file if "." or "," exists in suffix
If Mid(strFile, Len(strName) + 1, 1) = "-" And CSng(strSuffix) >= 0 And _
InStr(1, strSuffix, ",") = 0 And InStr(1, strSuffix, ".") = 0 Then
'Store the max suffix
If CInt(strSuffix) >= intMax Then intMax = CInt(strSuffix)
End If
NextFile:
strFile = Dir
Loop
GetNewSuffix = intMax + 1
Exit Function

ErrorHandler:
If Err Then
Err.Clear
Resume NextFile
End If
End Function

mdmackillop
04-07-2008, 03:24 PM
Hi Rory,
I tried my best and eventually broke it by saving a few "Data" and then changing to "data". Is that trying too hard? Otherwise it handled all other name versions I tried.
Regards
Malcolm

rory
04-07-2008, 03:31 PM
How about this:
Function Get_Filename(StrFolderPath As String, DocName As String, Ext As String) As String
Dim myfilename As String, strPattern As String, strReplace As String
Dim lngNewNum As Long, lngMaxNum As Long
Dim fso As Object
Dim fdr As Object, fls As Object, fil As Object
Dim regex As Object, objMatchCol As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(StrFolderPath & "\" & DocName & Ext) Then
Set fls = fso.GetFolder(StrFolderPath).Files
strPattern = "^" & DocName & " ?- ?\d+" & Ext & "$"
strReplace = "^" & DocName & " ?- ?"
Set regex = CreateObject("vbscript.regexp")
With regex
.Global = True
.IgnoreCase = True
For Each fil In fls
.Pattern = strPattern
Set objMatchCol = .Execute(fil.Name)
If objMatchCol.Count Then
.Pattern = strReplace
lngNewNum = Val(.Replace(fil.Name, ""))
If lngNewNum > lngMaxNum Then lngMaxNum = lngNewNum
End If
Next fil
End With
Get_Filename = DocName & "-" & lngMaxNum + 1 & Ext
Else
Get_Filename = DocName & Ext
End If
End Function

mdmackillop
04-07-2008, 04:05 PM
Hi tstav,
Yours handled all the variations I could think of. Personally I'd be inclined to add

If InStr(1, strName, "-") Then MsgBox "Don't use '-' in the name"

or automatically change "-" to an underscore, which would simplify the code to determine the suffix.

Maybe we should use _1 , |1 or :1 instead of -1. Actually, the 01 format would be better to allow alphanumeric sorting. I'm happy to resolve these bits for my own use.

Thanks to all who've contributed here. A couple of good solutions in my opinion.

Edit: I guessed you would quickly fix the Case Rory.

tstav
04-07-2008, 04:13 PM
Hi rory,

This is a part of the list of files against which I'm testing.
01.Data.xls
02.Data-1.xls
03.Data-2.xls
04.Da.ta.xls
05.Data2.xls
06.Data3.xls
07.Data4.xls
08.Data-3.5.xls
09.Data-4,5.xls
10.Data-a6.xls
11.Data -5.xls
12.Data - .xls
13.Datao.xls
14.Data o.xls

Among these files I consider as valid (files that I need to take into account) only the second and the third, since
1. we definitely want the new file to have an increment and
2. these are the only files that have a valid (integer) increment

Now, files saved from the application would always produce integer increments.
If a user ever deleted any of these files (creating increment gaps) I should cater for that and avoid producing an already existing filename.
If a user changed the name of any of the files (like Data -5.xls, Datao-5.xls, Data-4,5.xls)... in any way that it would alter either the 'root' name (Data) or the integer increment to sth not integer, I'd stop taking this file into account.

Testing your version with "Data" against this sample produced "Data-6.xls".
Mine gave "Data-3.xls"

I'll keep testing.

Best regards, tstav

tstav
04-07-2008, 04:28 PM
My pleasure Malcolm,
I'll check out your new hints, too.

tstav