PathManipulate – A custom .Net Class for displaying long file paths in different formats


Here is a Class that can be used to display long filepaths in different formats depending on your needs. You can specify a desired length, the location of ellipses, and whether the filename is always displayed. It can also be used to return only the filename (with extension) when passed a full path.

Updates will be made to it as they’re available.

Hope someone finds it useful. Any efficiencies that can be made, let me know!

PathManipulate Screenshot

Download the .vb file: PathManipulate.vb (Right click and “Save Target As”)

Source Code:

”’

”’ Class allows the formatting of filepaths for displaying in different formats. All methods return a String object. ”’

Public Class PathManipulate

”’

”’ Returns the folder path that the supplied full filenpath resides in. Includes trailing ‘\’ ”’

Public Function ShowDirectory(ByVal LongPath As String)
Dim FilePath As String
‘START AT BEGINNING OF FULL PATH AND END AT THE LAST OCCURRENCE OF ‘\’
FilePath = LongPath.Substring(0, LongPath.LastIndexOf(“\”) + 1)

Return FilePath
End Function

”’

”’ Returns only the filename (with extension) of the supplied filepath. Removes leading ‘\’. ”’

Public Function ShowFilenameOnly(ByVal LongPath As String)
Dim FileName As String
FileName = LongPath.Substring(LongPath.LastIndexOf(“\”) + 1, LongPath.Length – LongPath.LastIndexOf(“\”) – 1)

Return FileName
End Function

”’

”’ Returns filepath shortened with ellipses in the middle. ”’

Public Function ShrinkLongFilepath(ByVal LongPath As String) As String
Dim ReturnPath, StartPath, FileName As String
FileName = LongPath.Substring(LongPath.LastIndexOf(“\”) + 1, LongPath.Length – LongPath.LastIndexOf(“\”) – 1)

‘ELLIPSES WILL BE IN THE MIDDLE
StartPath = LongPath.Substring(0, LongPath.Length – LongPath.LastIndexOf(“\”) – 3)
ReturnPath = StartPath & “…\” & FileName

Return ReturnPath
End Function

”’

”’ Returns filepath shortened to desired length, if supplied, with ellipses in middle unless overridden with parameter. ”’

Public Function ShrinkLongFilepath(ByVal LongPath As String, ByVal EllipsesAtEnd As Boolean, Optional ByVal DesiredLength As Integer = 15, Optional ByVal AlwaysDisplayFilename As Boolean = False) As String
Dim ReturnPath, StartPath, EndPath, FileName As String

‘DESIRED LENGTH MUST BE AT LEAST 15
If DesiredLength < 15 Then DesiredLength = 15 End If 'STRIPS ONLY THE FILENAME FileName = LongPath.Substring(LongPath.LastIndexOf("\"), LongPath.Length - LongPath.LastIndexOf("\")) If AlwaysDisplayFilename Then If FileName.Length + 3 < DesiredLength Then 'ELLIPSES WILL BE IN THE MIDDLE StartPath = LongPath.Substring(0, (DesiredLength / 2) - 3) EndPath = LongPath.Substring(StartPath.Length, DesiredLength - StartPath.Length) ReturnPath = StartPath &amp; "..." &amp; FileName Else ReturnPath = "..." &amp; FileName End If Else If EllipsesAtEnd Then 'IF ELLIPSES AT END OF FILEPATH ReturnPath = LongPath.Substring(0, DesiredLength - 3) ReturnPath &amp;= "..." Else 'ELLIPSES WILL BE IN THE MIDDLE If LongPath.Length < DesiredLength Then StartPath = LongPath.Substring(0, LongPath.IndexOf("\") + 1) EndPath = LongPath.Substring(StartPath.Length + 3, LongPath.Length - (StartPath.Length + 3)) ReturnPath = StartPath &amp; "..." &amp; EndPath Else StartPath = LongPath.Substring(0, LongPath.IndexOf("\") + 1) EndPath = LongPath.Substring(StartPath.Length + 3, DesiredLength - (StartPath.Length + 3)) ReturnPath = StartPath &amp; "..." &amp; EndPath End If End If End If Return ReturnPath End Function End Class [/code] Some sample usage: [code lang="vb.net"] Dim pm As New PathManipulate 'Create and instantiate a new PathManipulate Object Dim di As New DirectoryInfo(folderpath) 'A full folderpath, possibly from a FolderBrowserDialog Dim myFiles As FileInfo() = di.GetFiles("*.*") Dim fi As FileInfo For Each fi In myFiles ListBox1.Items.Add(fi.FullName) 'Shows unaltered full path ListBox2.Items.Add(pm.ShowDirectory(fi.FullName)) 'Adds manipulated path Next 'Quick usage. Takes a full path and shrinks it. pm.ShrinkLongFilepath(fi.FullName) 'Shrinks to 40 characters with ellipses in the middle. pm.ShrinkLongFilepath(fi.FullName, False, 40, False) 'Returns the full directory path for the supplied file. pm.ShowDirectory(fi.FullName) 'Shows filenames regardless of desired length. pm.ShrinkLongFilepath(fi.FullName, False, 20, True) 'Shrink to 20 characters with ellipses at the end. pm.ShrinkLongFilepath(fi.FullName, True, 20, False) [/code]
Comments welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *