Solved

Truncate a filename

Posted on 2004-10-04
3
383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 …
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…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

724 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