Solved

Truncate a filename

Posted on 2004-10-04
3
377 Views
Last Modified: 2008-02-01
Hi

THis may be fairly simple but i am having real trouble finding an answer.  I would like to get the truncated filename for a file once a user has imported the file into my windows ap.

eg:

    If OpenFileDialog1.ShowDialog = DialogResult.OK Then
      txtDataFile.Text = OpenFileDialog1.FileName
    End If

returns eg: c:\documents and settings\user\this is a great program.exe

i need to have this in the truncated (i think thats what its called) version

eg: c:\documen~1\user\this~1.exe (or something like that)

and also get the name of the file itself from this

thanks very much

Matt
0
Comment
Question by:cafferm
3 Comments
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 12223998
Well I do not know of any direct method of getting the truncated version of File Path, However you can write a function to get the desired result.

Here it is for you
You can pass the full Path to the Function TruncateFileName and it will return the Truncated file Name (I have not done a thorough testing of the code but it should work)
Actually the Theory is that the DOS version of filename has at the most 8 chars excluding the file path, so in case length of any folder in the path is more than 8 I am truncating it at 6 and adding a Tilde (~) and a suitable digit 1 or 2 based on the folder name's occurance in the path

Private Function TruncateFileName(ByVal FileName As String) As String
        Dim Arr() As String
        Dim FName As String

        FName = FileName
        TruncateFileName = FileName.Substring(0, FileName.IndexOf("\"))
        FName = FileName.Remove(0, FileName.IndexOf("\") + 1)
        Arr = Split(FName, "\")
        Dim i As Integer
        For i = 0 To Arr.Length - 1
            TruncateFileName &= "\"
            If (Arr(i).IndexOf(".") < 0 AndAlso Arr(i).Length > 8) Or (Arr(i).IndexOf(".") > 0 AndAlso Arr(i).Length > 12) Then
                TruncateFileName &= Arr(i).Substring(0, 6)
                TruncateFileName &= "~"
                TruncateFileName &= GetEnding(Arr, i)
            Else
                TruncateFileName &= Arr(i)
            End If
        Next
        Return TruncateFileName
    End Function

    Private Function GetEnding(ByVal Arr() As String, ByVal Index As Integer) As String
        Dim Tmp As String = Arr(Index).Substring(0, 6).ToUpper
        Dim Ct As Int16 = 1
        Dim i As Integer
        For i = 0 To Index - 1
            If Arr(i).Length > 6 Then
                If (Tmp = Arr(i).Substring(0, 6).ToUpper) Then Ct += 1
            End If
        Next
        i = Arr(Index).IndexOf(".")
        If i > 0 Then
            GetEnding = CStr(Ct) & Arr(Index).Substring(i, Arr(Index).Length - i)
        Else
            GetEnding = CStr(Ct)
        End If
        Return GetEnding
    End Function
0
 
LVL 5

Accepted Solution

by:
KarcOrigin earned 250 total points
ID: 12224005
Try this:
In module level diclare this:

Option Explicit
Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

In form level do this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

End Sub


Private Sub Command1_Click()
Dim spath As String = Space(255)
Dim FileN As String = Space(255)

    With CommonDialog1
        .ShowOpen
        Dim l As Long
        l = GetShortPathName(.FileName, spath, 255)
        MsgBox spath
        l = GetShortPathName(.FileTitle, FileN, 255)
        MsgBox FileN
    End With
End Sub
0
 

Author Comment

by:cafferm
ID: 12224030
Excellent Thanks KarcOrigin thats exactly what i wanted

arif_eqbal thanks very much, the one liner does it everytime
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now