Trim a File Path for Display in a Text Box

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)) _ 0) And _iPos3 + Len(Mid$(sFileName, _iPos))  0) And _iPos3 + Len(Mid$(sFileName, iPos)) _ 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

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist