Tag Archives: filename

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

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)

 

Extract and get/retrieve the Filename only from a path using VB.NET

One Way to do It


'
'This will extract and return the filename from the specified path and filename.
'
Dim filePath As String = "c:\MyDirectory\MYFile.txt"
Dim slashPosition As Integer = filePath.LastIndexOf("\")
Dim filenameOnly As String = filePAth.Substring(slashPosition + 1)

MsgBox(filenameOnly)

 

Second Way to do It. The better way actually.

'
'Thanks to Jim for this code as it is actually a easier way than the original code I
'posted. Just remember that this code will INCLUDE the extension. So you will need to
'add code to remove it if you don't want the files extension.
Dim fullPath As String = "c:\MyDirectory\MYFile.txt"
Dim dirName As String = IO.Path.GetFileName(fullPath)

    MsgBox(dirName)