Filesystem: Add GetVersionedString

This commit is contained in:
Jonathan Jenne 2022-04-28 10:56:51 +02:00
parent e3ee11c6a6
commit a13deeddf2
2 changed files with 64 additions and 20 deletions

View File

@ -120,7 +120,6 @@ Public Class File
Dim oExtension = Path.GetExtension(oFileName) Dim oExtension = Path.GetExtension(oFileName)
Dim oVersionSeparator As Char = "~"c Dim oVersionSeparator As Char = "~"c
Dim oFileVersion As Integer = Nothing
' Split Filename without extension at version separator to: ' Split Filename without extension at version separator to:
' - Check if file is already versioned ' - Check if file is already versioned
@ -130,25 +129,31 @@ Public Class File
' test1.pdf --> test1 --> ['test1'] --> no fileversion ' test1.pdf --> test1 --> ['test1'] --> no fileversion
' test1~2.pdf --> test1~2 --> ['test1', '2'] --> version 2 ' test1~2.pdf --> test1~2 --> ['test1', '2'] --> version 2
' test1~12345~2.pdf --> test1~12345~2 --> ['test1', '12345', '2'] --> still version 2 ' test1~12345~2.pdf --> test1~12345~2 --> ['test1', '12345', '2'] --> still version 2
Dim oFileNameWithoutExtension = Path.GetFileNameWithoutExtension(oFileName) 'Dim oFileNameWithoutExtension = Path.GetFileNameWithoutExtension(oFileName)
Dim oSplitFilename = oFileNameWithoutExtension.Split(oVersionSeparator).ToList() 'Dim oSplitFilename = oFileNameWithoutExtension.Split(oVersionSeparator).ToList()
' if file is already versioned, extract file version ' if file is already versioned, extract file version
' else just use the filename and set version to 1 ' else just use the filename and set version to 1
If oSplitFilename.Count > 1 Then 'If oSplitFilename.Count > 1 Then
Dim oVersion As Integer = 1 ' Dim oVersion As Integer = 1
Try ' Try
oVersion = Integer.Parse(oSplitFilename.Last()) ' oVersion = Integer.Parse(oSplitFilename.Last())
oFileNameWithoutExtension = String.Join("", oSplitFilename.Take(oSplitFilename.Count - 1)) ' oFileNameWithoutExtension = String.Join("", oSplitFilename.Take(oSplitFilename.Count - 1))
Catch ex As Exception ' Catch ex As Exception
' oFilenameWithoutExtension does NOT change ' ' oFilenameWithoutExtension does NOT change
oFileNameWithoutExtension = oFileNameWithoutExtension ' oFileNameWithoutExtension = oFileNameWithoutExtension
Finally ' Finally
oFileVersion = oVersion ' oFileVersion = oVersion
End Try ' End Try
Else 'Else
oFileVersion = 1 ' oFileVersion = 1
End If 'End If
Dim oFileNameWithoutExtension = Path.GetFileNameWithoutExtension(oFileName)
Dim oSplitResult = GetVersionedString(oFileNameWithoutExtension, oVersionSeparator)
oFileNameWithoutExtension = oSplitResult.Item1
Dim oFileVersion = oSplitResult.Item2
' 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.
@ -180,6 +185,45 @@ Public Class File
End Try End Try
End Function End Function
''' <summary>
''' Split String at version separator to:
''' check if string is already versioned,
''' get the string version of an already versioned string
''' </summary>
''' <example>
''' Examples:
''' test1.pdf --> test1 --> ['test1'] --> no fileversion
''' test1~2.pdf --> test1~2 --> ['test1', '2'] --> version 2
''' test1~12345~2.pdf --> test1~12345~2 --> ['test1', '12345', '2'] --> still version 2
''' somestring~3 --> somestring~3 --> ['somestring', '3'] --> version 3
''' </example>
''' <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()
Dim oStringVersion As Integer
' if string is already versioned, extract string version
' else just use the string and set version to 1
If oSplitString.Count > 1 Then
Dim oVersion As Integer = 1
Try
oVersion = Integer.Parse(oSplitString.Last())
pString = String.Join("", oSplitString.Take(oSplitString.Count - 1))
Catch ex As Exception
' pString does NOT change
pString = pString
Finally
oStringVersion = oVersion
End Try
Else
oStringVersion = 1
End If
Return New Tuple(Of String, Integer)(pString, oStringVersion)
End Function
Public Function GetAppDataPath(CompanyName As String, ProductName As String) Public Function GetAppDataPath(CompanyName As String, ProductName As String)
Dim oLocalAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) Dim oLocalAppData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Return Path.Combine(oLocalAppData, CompanyName, ProductName) Return Path.Combine(oLocalAppData, CompanyName, ProductName)

View File

@ -13,7 +13,7 @@ Imports System.Runtime.InteropServices
<Assembly: AssemblyCompany("Digital Data")> <Assembly: AssemblyCompany("Digital Data")>
<Assembly: AssemblyProduct("Modules.Filesystem")> <Assembly: AssemblyProduct("Modules.Filesystem")>
<Assembly: AssemblyCopyright("Copyright © 2022")> <Assembly: AssemblyCopyright("Copyright © 2022")>
<Assembly: AssemblyTrademark("1.3.0.0")> <Assembly: AssemblyTrademark("1.3.1.0")>
<Assembly: ComVisible(False)> <Assembly: ComVisible(False)>
@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' übernehmen, indem Sie "*" eingeben: ' übernehmen, indem Sie "*" eingeben:
' <Assembly: AssemblyVersion("1.0.*")> ' <Assembly: AssemblyVersion("1.0.*")>
<Assembly: AssemblyVersion("1.3.0.0")> <Assembly: AssemblyVersion("1.3.1.0")>
<Assembly: AssemblyFileVersion("1.3.0.0")> <Assembly: AssemblyFileVersion("1.3.1.0")>