Extract/Return the file Path and Filename without the Extension using VB.NET

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

This will take a file path, extract and return the filesystem path along with the filename but WITHOUT the extension.

        Dim filePath As String = "c:\MyDirectory\MYFile.txt"

        Dim extensionPosition As Integer = filePath.LastIndexOf(".")

        Dim filePathNoEx As String = filePath.Substring(0, extensionPosition)

        MsgBox(filePathNoEx)

 

3 thoughts on “Extract/Return the file Path and Filename without the Extension using VB.NET

  1. Jason H

    Hi, This snippet is for someone that WANTs the “Path AND Filename” but NOT the Extension for the filename.

    The code you posted could be used for checking the position of the last slash if you wanted – “the path without the filename” or “filename without the path”.

    But I did go ahead and change the slashPosition variable in this code snippet to “extensionPosition” since it sounds more relevent.

    I do thankyou for the comment too 🙂

    Jason

    Reply

Leave a Reply