Solved

Truncate a filename

Posted on 2004-10-04
3
376 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
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent Thanks KarcOrigin thats exactly what i wanted

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

18 Experts available now in Live!

Get 1:1 Help Now