Get or Extract/Trim the extension from a filename and path in VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
 

This will remove the extension from the filepath and return the extension only. There are two methods of doing it below. If you don’t care that the . (period) is included with the extension then use the second way. This works for all versions of .NET including Visual Basic 2010.

Here is One way to do it…

'
'This will extract and return the extension without the dot from a specified filename/path.
'
Dim filePath As String = "c:\MyDirectory\MY.File.jpeg"
Dim dotPosition As Integer = filePath.LastIndexOf(".")
Dim extOnly As String = filePath.Substring(dotPosition + 1)

MsgBox(extOnly)


Another easy way from Jim. But it will include the “.” period with the string.

'
'Thanks to Jim for this other method to get the file extension. Just remember that the "."
'(dot or period) will be included with the extension. So you will need to add codes to remove
'it if you don't want the extension seperator character included.

Dim fullPath As String = "c:\MyDirectory\MYFile.txt"
Dim fileName As String = IO.Path.GetExtension(fullPath)

MsgBox(fileName)

 

Have fun 🙂

6 thoughts on “Get or Extract/Trim the extension from a filename and path in VB.NET

  1. Jim

    how about using the IO object

    Dim fullPath As String = "c:\MyDirectory\MYFile.txt"
    
    Dim fileName As String = IO.Path.GetExtension(fullPath)
    
    Reply
  2. Jason H

    Yes, you are absolutely right. I actually recently used code similar to yours in a application example i’m going to be adding to my vbcodesource.com page.

    Thankyou for the info. I will add it to the post when I get the chance. 🙂

    Jason

    Reply
  3. john

    if you want to extract the last three characters of a string after the last dot you might want to do it like this:

    Function get_extension(sFilename) As String
    Dim iChar As Integer
    Dim iLen As Integer
    Dim sChar As String
    Dim iExtStart As Integer
    Dim lDone As Boolean
    lDone = False
    iLen = Len(sFilename)
    iChar = iLen
    get_extension = “”
    If iLen > 0 Then
    While Not lDone
    sChar = Mid(sFilename, iChar, 1)
    If sChar = “.” Then
    If iChar = iLen Then
    get_extension = “”
    lDone = True
    Else
    get_extension = Mid(sFilename, iChar + 1)
    lDone = True
    End
    Else
    i = i – 1
    If i = 0 Then
    lDone = True
    End If
    End If
    Wend
    End If
    End Function

    Reply
  4. Kuuri

    Another way….

    Private Function GetExtension(path As String) As String
    
        Dim Dotpos As Integer
        Dim NoDot() As String
        NoDot = Split(path, ".")
        Dim Extension As String = NoDot(UBound(NoDot))
    
        MsgBox(Extension)
    
    End Function
    
    Reply
  5. anon

    john,

    Not all files are simply 3 character extensions is the problem. The case of a simple jpeg could completely throw your program off the wrong direction.

    Reply

Leave a Reply