Solved

Truncate a filename

Posted on 2004-10-04
3
379 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…

821 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