Fix GetVersionedFileName
This commit is contained in:
parent
ec309b5afb
commit
7fea3dc1ff
@ -41,6 +41,7 @@ Public Class File
|
||||
|
||||
' This prevents an infinite loop when no file can be created in a location
|
||||
Private Const MAX_FILE_VERSION = 100
|
||||
Private Const VERSION_SEPARATOR As Char = "~"c
|
||||
|
||||
Private Const FILE_NAME_ACCESS_TEST = "accessTest.txt"
|
||||
|
||||
@ -123,7 +124,7 @@ Public Class File
|
||||
''' <param name="pFilePath">Filepath to check</param>
|
||||
''' <returns>Versioned string</returns>
|
||||
Public Function GetVersionedFilename(pFilePath As String) As String
|
||||
Return GetVersionedFilenameWithFilecheck(pFilePath, Function(pPath As String) IO.File.Exists(pFilePath))
|
||||
Return GetVersionedFilenameWithFilecheck(pFilePath, Function(pPath As String) IO.File.Exists(pPath))
|
||||
End Function
|
||||
|
||||
''' <summary>
|
||||
@ -140,14 +141,11 @@ Public Class File
|
||||
Dim oDestinationDir = Path.GetDirectoryName(oFileName)
|
||||
Dim oExtension = Path.GetExtension(oFileName)
|
||||
|
||||
Dim oVersionSeparator As Char = "~"c
|
||||
|
||||
|
||||
Dim oFileNameWithoutExtension = Path.GetFileNameWithoutExtension(oFileName)
|
||||
Dim oSplitResult = GetVersionedString(oFileNameWithoutExtension, oVersionSeparator)
|
||||
Dim oSplitResult = GetVersionedString(oFileNameWithoutExtension)
|
||||
|
||||
oFileNameWithoutExtension = oSplitResult.Item1
|
||||
Dim oFileVersion = oSplitResult.Item2
|
||||
Dim oFileVersion As Integer = oSplitResult.Item2
|
||||
|
||||
' Shorten the filename (only filename, without extension or version)
|
||||
' by cutting the length in half. This should work no matter how long the path and/or filename are.
|
||||
@ -165,15 +163,15 @@ Public Class File
|
||||
' while file exists, increment version.
|
||||
' version cannot go above MAX_FILE_VERSION, to prevent infinite loop
|
||||
Do
|
||||
If oFileVersion >= MAX_FILE_VERSION Then
|
||||
Throw New OverflowException($"Tried '{MAX_FILE_VERSION}' times to version filename before giving up. Sorry.")
|
||||
End If
|
||||
|
||||
oFinalFileName = Path.Combine(oDestinationDir, GetFilenameWithVersion(oFileNameWithoutExtension, oVersionSeparator, oFileVersion, oExtension))
|
||||
oFinalFileName = Path.Combine(oDestinationDir, GetFilenameWithVersion(oFileNameWithoutExtension, oFileVersion, oExtension))
|
||||
_Logger.Debug("Intermediate Filename is {0}", oFinalFileName)
|
||||
_Logger.Debug("File version: {0}", oFileVersion)
|
||||
oFileVersion += 1
|
||||
Loop While pFileExistsAction(oFinalFileName) = True
|
||||
Loop While pFileExistsAction(oFinalFileName) = True And oFileVersion < MAX_FILE_VERSION
|
||||
|
||||
If oFileVersion >= MAX_FILE_VERSION Then
|
||||
Throw New OverflowException($"Tried '{MAX_FILE_VERSION}' times to version filename before giving up. Sorry.")
|
||||
End If
|
||||
|
||||
_Logger.Debug("Final Filename is {0}", oFinalFileName)
|
||||
|
||||
@ -200,8 +198,8 @@ Public Class File
|
||||
''' <param name="pString">The string to versioned</param>
|
||||
''' <param name="pSeparator">The character to split at</param>
|
||||
''' <returns>Tuple of string and version</returns>
|
||||
Public Function GetVersionedString(pString As String, pSeparator As Char) As Tuple(Of String, Integer)
|
||||
Dim oSplitString = pString.Split(pSeparator).ToList()
|
||||
Public Function GetVersionedString(pString As String) As Tuple(Of String, Integer)
|
||||
Dim oSplitString = pString.Split(VERSION_SEPARATOR).ToList()
|
||||
Dim oStringVersion As Integer
|
||||
|
||||
' if string is already versioned, extract string version
|
||||
@ -231,11 +229,11 @@ Public Class File
|
||||
Return Path.Combine(oLocalAppData, CompanyName, ProductName)
|
||||
End Function
|
||||
|
||||
Private Function GetFilenameWithVersion(FileNameWithoutExtension As String, VersionSeparator As Char, FileVersion As Integer, Extension As String) As String
|
||||
Private Function GetFilenameWithVersion(FileNameWithoutExtension As String, FileVersion As Integer, Extension As String) As String
|
||||
If FileVersion <= 1 Then
|
||||
Return $"{FileNameWithoutExtension}{Extension}"
|
||||
Else
|
||||
Return $"{FileNameWithoutExtension}{VersionSeparator}{FileVersion}{Extension}"
|
||||
Return $"{FileNameWithoutExtension}{VERSION_SEPARATOR}{FileVersion}{Extension}"
|
||||
End If
|
||||
End Function
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user