Filesystem: Add new method GetVersionedFilenameWithFileCheck
This commit is contained in:
@@ -109,11 +109,21 @@ Public Class File
|
|||||||
''' <summary>
|
''' <summary>
|
||||||
''' Adds file version string to given filename `Destination` if that file already exists.
|
''' Adds file version string to given filename `Destination` if that file already exists.
|
||||||
''' </summary>
|
''' </summary>
|
||||||
''' <param name="Destination"></param>
|
''' <param name="pFilePath">Filepath to check</param>
|
||||||
''' <returns></returns>
|
''' <returns>Versioned string</returns>
|
||||||
Public Function GetVersionedFilename(Destination As String) As String
|
Public Function GetVersionedFilename(pFilePath As String) As String
|
||||||
|
Return GetVersionedFilenameWithFilecheck(pFilePath, Function(pPath As String) IO.File.Exists(pFilePath))
|
||||||
|
End Function
|
||||||
|
|
||||||
|
''' <summary>
|
||||||
|
''' Adds file version string to given filename `Destination` if that file already exists.
|
||||||
|
''' </summary>
|
||||||
|
''' <param name="pFilePath">Filepath to check</param>
|
||||||
|
''' <param name="pFileExistsAction">Custom action to check for file existence</param>
|
||||||
|
''' <returns>Versioned string</returns>
|
||||||
|
Public Function GetVersionedFilenameWithFilecheck(pFilePath As String, pFileExistsAction As Func(Of String, Boolean)) As String
|
||||||
Try
|
Try
|
||||||
Dim oFileName As String = Destination
|
Dim oFileName As String = pFilePath
|
||||||
Dim oFinalFileName = oFileName
|
Dim oFinalFileName = oFileName
|
||||||
|
|
||||||
Dim oDestinationDir = Path.GetDirectoryName(oFileName)
|
Dim oDestinationDir = Path.GetDirectoryName(oFileName)
|
||||||
@@ -131,7 +141,7 @@ Public Class File
|
|||||||
' Shorten the filename (only filename, without extension or version)
|
' 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.
|
' by cutting the length in half. This should work no matter how long the path and/or filename are.
|
||||||
' The initial check operates on the full path to catch all scenarios.
|
' The initial check operates on the full path to catch all scenarios.
|
||||||
If Destination.Length > MAX_FILE_PATH_LENGTH Then
|
If pFilePath.Length > MAX_FILE_PATH_LENGTH Then
|
||||||
_Logger.Info("Filename is too long. Filename will be cut to prevent further errors.")
|
_Logger.Info("Filename is too long. Filename will be cut to prevent further errors.")
|
||||||
_Logger.Info("Original Filename is: {0}", oFileNameWithoutExtension)
|
_Logger.Info("Original Filename is: {0}", oFileNameWithoutExtension)
|
||||||
Dim oNewLength As Integer = Math.Round(oFileNameWithoutExtension.Length / 2)
|
Dim oNewLength As Integer = Math.Round(oFileNameWithoutExtension.Length / 2)
|
||||||
@@ -147,15 +157,15 @@ Public Class File
|
|||||||
_Logger.Debug("Intermediate Filename is {0}", oFinalFileName)
|
_Logger.Debug("Intermediate Filename is {0}", oFinalFileName)
|
||||||
_Logger.Debug("File version: {0}", oFileVersion)
|
_Logger.Debug("File version: {0}", oFileVersion)
|
||||||
oFileVersion += 1
|
oFileVersion += 1
|
||||||
Loop While (IO.File.Exists(oFinalFileName))
|
Loop While pFileExistsAction(oFinalFileName) = True
|
||||||
|
|
||||||
_Logger.Debug("Final Filename is {0}", oFinalFileName)
|
_Logger.Debug("Final Filename is {0}", oFinalFileName)
|
||||||
|
|
||||||
Return oFinalFileName
|
Return oFinalFileName
|
||||||
Catch ex As Exception
|
Catch ex As Exception
|
||||||
_Logger.Warn("Filename {0} could not be versioned. Original filename will be returned!", Destination)
|
_Logger.Warn("Filename {0} could not be versioned. Original filename will be returned!", pFilePath)
|
||||||
_Logger.Error(ex)
|
_Logger.Error(ex)
|
||||||
Return Destination
|
Return pFilePath
|
||||||
End Try
|
End Try
|
||||||
End Function
|
End Function
|
||||||
|
|
||||||
@@ -195,6 +205,8 @@ Public Class File
|
|||||||
oStringVersion = 1
|
oStringVersion = 1
|
||||||
End If
|
End If
|
||||||
|
|
||||||
|
_Logger.Debug("Versioned: String [{0}], Version [{1}]", pString, oStringVersion)
|
||||||
|
|
||||||
Return New Tuple(Of String, Integer)(pString, oStringVersion)
|
Return New Tuple(Of String, Integer)(pString, oStringVersion)
|
||||||
End Function
|
End Function
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user