Results 1 to 1 of 1

Thread: PathManipulate - File Path Manipulation Class (For Varied Display Options)

Threaded View

  1. #1

    Thread Starter
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    PathManipulate - File Path Manipulation Class (For Varied Display Options)

    As requested,
    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!

    Source Code:

    vb.net Code:
    1. ''' <summary>  
    2. ''' <para>Class allows the formatting of filepaths for displaying in different formats. All methods return a String object.</para>  
    3. ''' </summary>  
    4. Public Class PathManipulate
    5.  
    6.     ''' <summary>  
    7.     ''' <para>Returns the folder path that the supplied full filenpath resides in. Includes trailing '\'</para>  
    8.     ''' </summary>  
    9.     Public Function ShowDirectory(ByVal LongPath As String)
    10.         Dim FilePath As String
    11.         'START AT BEGINNING OF FULL PATH AND END AT THE LAST OCCURRENCE OF '\'
    12.         FilePath = LongPath.Substring(0, LongPath.LastIndexOf("\") + 1)
    13.  
    14.         Return FilePath
    15.     End Function
    16.  
    17.     ''' <summary>  
    18.     ''' <para>Returns only the filename (with extension) of the supplied filepath. Removes leading '\'.</para>  
    19.     ''' </summary>  
    20.     Public Function ShowFilenameOnly(ByVal LongPath As String)
    21.         Dim FileName As String
    22.         FileName = LongPath.Substring(LongPath.LastIndexOf("\") + 1, LongPath.Length - LongPath.LastIndexOf("\") - 1)
    23.  
    24.         Return FileName
    25.     End Function
    26.  
    27.     ''' <summary>  
    28.     ''' <para>Returns filepath shortened with ellipses in the middle.</para>  
    29.     ''' </summary>  
    30.     Public Function ShrinkLongFilepath(ByVal LongPath As String) As String
    31.         Dim ReturnPath, StartPath, FileName As String
    32.         FileName = LongPath.Substring(LongPath.LastIndexOf("\") + 1, LongPath.Length - LongPath.LastIndexOf("\") - 1)
    33.  
    34.         'ELLIPSES WILL BE IN THE MIDDLE
    35.         StartPath = LongPath.Substring(0, LongPath.Length - LongPath.LastIndexOf("\") - 3)
    36.         ReturnPath = StartPath & "...\" & FileName
    37.  
    38.         Return ReturnPath
    39.     End Function
    40.  
    41.     ''' <summary>  
    42.     ''' <para>Returns filepath shortened to desired length, if supplied, with ellipses in middle unless overridden with parameter.</para>  
    43.     ''' </summary>  
    44.     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
    45.         Dim ReturnPath, StartPath, EndPath, FileName As String
    46.  
    47.         'DESIRED LENGTH MUST BE AT LEAST 15
    48.         If DesiredLength < 15 Then
    49.             DesiredLength = 15
    50.         End If
    51.  
    52.         'STRIPS ONLY THE FILENAME
    53.         FileName = LongPath.Substring(LongPath.LastIndexOf("\"), LongPath.Length - LongPath.LastIndexOf("\"))
    54.  
    55.         If AlwaysDisplayFilename Then
    56.             If FileName.Length + 3 < DesiredLength Then
    57.                 'ELLIPSES WILL BE IN THE MIDDLE
    58.                 StartPath = LongPath.Substring(0, (DesiredLength / 2) - 3)
    59.                 EndPath = LongPath.Substring(StartPath.Length, DesiredLength - StartPath.Length)
    60.                 ReturnPath = StartPath & "..." & FileName
    61.             Else
    62.                 ReturnPath = "..." & FileName
    63.             End If
    64.         Else
    65.             If EllipsesAtEnd Then 'IF ELLIPSES AT END OF FILEPATH
    66.                 ReturnPath = LongPath.Substring(0, DesiredLength - 3)
    67.                 ReturnPath &= "..."
    68.             Else 'ELLIPSES WILL BE IN THE MIDDLE
    69.                 If LongPath.Length < DesiredLength Then
    70.                     StartPath = LongPath.Substring(0, LongPath.IndexOf("\") + 1)
    71.                     EndPath = LongPath.Substring(StartPath.Length + 3, LongPath.Length - (StartPath.Length + 3))
    72.                     ReturnPath = StartPath & "..." & EndPath
    73.                 Else
    74.                     StartPath = LongPath.Substring(0, LongPath.IndexOf("\") + 1)
    75.                     EndPath = LongPath.Substring(StartPath.Length + 3, DesiredLength - (StartPath.Length + 3))
    76.                     ReturnPath = StartPath & "..." & EndPath
    77.                 End If
    78.             End If
    79.         End If
    80.  
    81.         Return ReturnPath
    82.     End Function
    83.  
    84. End Class

    Some sample usage:

    vb.net Code:
    1. Dim pm As New PathManipulate 'Create and instantiate a new PathManipulate Object
    2. Dim di As New DirectoryInfo(folderpath) 'A full folderpath, possibly from a FolderBrowserDialog
    3. Dim myFiles As FileInfo() = di.GetFiles("*.*")
    4. Dim fi As FileInfo
    5.  
    6. For Each fi In myFiles
    7.      ListBox1.Items.Add(fi.FullName) 'Shows unaltered full path
    8.      ListBox2.Items.Add(pm.ShowDirectory(fi.FullName)) 'Adds manipulated path
    9. Next
    10.  
    11. 'Quick usage. Takes a full path and shrinks it.
    12. pm.ShrinkLongFilepath(fi.FullName)
    13.  
    14. 'Shrinks to 40 characters with ellipses in the middle.
    15. pm.ShrinkLongFilepath(fi.FullName, False, 40, False)
    16.  
    17. 'Returns the full directory path  for the supplied file.
    18. pm.ShowDirectory(fi.FullName)
    19.  
    20. 'Shows filenames regardless of desired length.
    21. pm.ShrinkLongFilepath(fi.FullName, False, 20, True)
    22.  
    23. 'Shrink to 20 characters with ellipses at the end.
    24. pm.ShrinkLongFilepath(fi.FullName, True, 20, False)
    Attached Images Attached Images  

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width