Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 6, 1997

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 DIRECTORY\AND LONG SUBDIRECTORY\AND ANOTHER SUBDIRECTORY\AND 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 String
If Len(sFileName) Then
Dim iPos As Integer, iPos0 As Integer, _
iPos1 As Integer, iPos2 As Integer, _
iPos3 As Integer, iPos4 As Integer
iPos = SGBkwdInstrS(0, _
Left$(sFileName, Len(sFileName) - 1), "\")
iPos0 = InStr(sFileName, ":")
iPos1 = InStr(sFileName, "\")
iPos2 = InStr(iPos1, _
sFileName, "\"): iPos2 = iPos1 + iPos2
iPos3 = InStr(iPos2, sFileName, _
"\"): iPos3 = iPos2 + iPos3
iPos4 = InStr(iPos3, _
sFileName, "\"): iPos4 = iPos3 + iPos4
If Len(sFileName) > iMaxLen Then
If (iPos4 <> 0) And _
iPos4 +Len(Right(sFileName, iPos)) _
<= iMaxLen - 2 Then
sFileName = Left$(sFileName, _
iPos4) & "..." & Right(sFileName, _
Len(sFileName) - iPos)
ElseIf (iPos3 > 0) And _
iPos3 + Len(Mid$(sFileName, _
iPos)) <= iMaxLen - 2 Then
sFileName = Left$(sFileName, _
iPos3) & "..." & _
Right(sFileName, Len(sFileName) - iPos)
ElseIf (iPos3 > 0) And _
iPos3 + Len(Mid$(sFileName, iPos)) _
<= iMaxLen - 2 Then
sFileName = Left$(sFileName, iPos2) & _
"..." & Right(sFileName, _
Len(sFileName) - iPos)
Else
sFileName = Left$(sFileName, iPos0 + 1) _
& "..." & Right(sFileName, _
Len(sFileName) - iPos)
End If
End If
End If
TruncatePath = Left$(sFileName, Len(sFileName) - 1)
End Function
Function 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 Function
If (iStart = zero) Then iStart = 1
If (iStart >= (Len(sTarget))) Then iStart = Len(sTarget)
iStart = Len(sTarget) - iStart + 1
On Error Resume Next
For IPtr = iStart To 1 Step True
If (SPattern = Mid$(sTarget, IPtr, IPLen)) Then
'found it
SGBkwdInstrS = IPtr
Exit For
End If
Next IPtr
End Function
MicroHelp Uninstaller
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date