Trim a File Path for Display in a Text Box

With the advent of long file names in Windows 95, it may be necessary to display a trimmed version of a path in a text box or label. The following function takes in a long file path and creates a trimmed version. For example, C:MY VERY LONG DIRECTORYAND LONG SUBDIRECTORYAND ANOTHER SUBDIRECTORYAND LONG FILENAME.TXT becomes C:MY VERY LONG DIRECTORY…AND LONG FILENAME.TXT. This function takes the full path to the file along with the maximum length that can be displayed in the text box, label, etc. It uses SGBkwdInstrS to find backslashes. The function is supplied below TruncatePath in the sample:

 Private Function TruncatePath(ByVal sFileName _As String, iMaxLen as Integer) As StringIf Len(sFileName) ThenDim iPos As Integer, iPos0 As Integer, _iPos1 As Integer, iPos2 As Integer, _iPos3 As Integer, iPos4 As IntegeriPos = SGBkwdInstrS(0, _Left$(sFileName, Len(sFileName) - 1), "")iPos0 = InStr(sFileName, ":")iPos1 = InStr(sFileName, "")iPos2 = InStr(iPos1, _sFileName, ""): iPos2 = iPos1 + iPos2iPos3 = InStr(iPos2, sFileName, _""): iPos3 = iPos2 + iPos3iPos4 = InStr(iPos3, _sFileName, ""): iPos4 = iPos3 + iPos4If Len(sFileName) > iMaxLen ThenIf (iPos4 <> 0) And _iPos4 +Len(Right(sFileName, iPos)) _<= iMaxLen - 2 ThensFileName = Left$(sFileName, _iPos4) & "..." & Right(sFileName, _Len(sFileName) - iPos)ElseIf (iPos3 > 0) And _iPos3 + Len(Mid$(sFileName, _iPos)) <= iMaxLen - 2 ThensFileName = Left$(sFileName, _iPos3) & "..." & _Right(sFileName, Len(sFileName) - iPos)ElseIf (iPos3 > 0) And _iPos3 + Len(Mid$(sFileName, iPos)) _<= iMaxLen - 2 ThensFileName = Left$(sFileName, iPos2) & _"..." & Right(sFileName, _Len(sFileName) - iPos)ElsesFileName = Left$(sFileName, iPos0 + 1) _& "..." & Right(sFileName, _Len(sFileName) - iPos)End IfEnd IfEnd IfTruncatePath = Left$(sFileName, Len(sFileName) - 1)End FunctionFunction SGBkwdInstrS(ByVal iStart As Integer, _ByVal sTarget As String, ByVal SPattern As String)Dim IPtr As Integer, IPLen As Integer IPLen = Len(SPattern)If ((Len(sTarget) = zero) Or (IPLen = zero) Or (Len(SPattern) > Len(sTarget))) Then Exit FunctionIf (iStart = zero) Then iStart = 1If (iStart >= (Len(sTarget))) Then iStart = Len(sTarget)iStart = Len(sTarget) - iStart + 1On Error Resume NextFor IPtr = iStart To 1 Step TrueIf (SPattern = Mid$(sTarget, IPtr, IPLen)) Then'found itSGBkwdInstrS = IPtrExit ForEnd IfNext IPtrEnd Function
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: