Frage

So I know how to increment. I have the following code working:

Dim startFileName As String = StorageRoot & endFile
Dim endFileName As String = String.Empty
Dim Counter As Integer = 1

Do
    Dim myFileInfo As New FileInfo(startFileName)
    endFileName = myFileInfo.DirectoryName & IIf(Not myFileInfo.DirectoryName.EndsWith("\"), "\", String.Empty).ToString & _
                  Path.GetFileNameWithoutExtension(startFileName) & Counter & myFileInfo.Extension
    Counter += 1
Loop Until Not IO.File.Exists(endFileName)

endFile = endFileName

This works. But here's my dilemma. Lets say the following files exist:

filename_v1.ext filename_v2.ext filename_v3.ext filename_v4.ext

And then the user deletes one of them, lets say filename_v3.ext

On the next upload with my above logic, I need a way for it to make it v5 and not v3 again. The way it is now, it loops and stops after v2 and creates a v3 again.

Thanks for the help!

War es hilfreich?

Lösung

You can get an array of all files that match filename_v*.ext from Directory.GetFiles(path, searchPattern).

Dim files As String() = Directory.GetFiles("c:\SomePath", "filename_v*.ext")

Parse out the numbers after _v, order them, then take the highest + 1.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top