Solved

Truncate a filename

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

10 Experts available now in Live!

Get 1:1 Help Now