Imports System
Imports System.IO
Module Program
''' <summary>
''' Removes the last file extension from a filename or path.
''' </summary>
''' <param name="filePath">
''' The filename or full path.
''' </param>
''' <returns>
''' The filename/path without its last extension.
''' </returns>
'''
''' Examples:
''' document.pdf -> document
''' archive.tar.gz -> archive.tar
''' photo.jpg -> photo
''' README -> README
''' C:\Temp\file.txt -> C:\Temp\file
Function RemoveFileExtension(filePath As String) As String
' Validate input.
If String.IsNullOrWhiteSpace(filePath) Then
Throw New ArgumentException(
"File path cannot be null or empty.",
NameOf(filePath))
End If
' Get the directory portion (if any).
Dim directory As String = Path.GetDirectoryName(filePath)
' Get the filename without its last extension.
Dim filenameWithoutExtension As String =
Path.GetFileNameWithoutExtension(filePath)
' If there is no directory, return only the filename.
If String.IsNullOrEmpty(directory) Then
Return filenameWithoutExtension
End If
' Reconstruct the path without the extension.
Return Path.Combine(directory, filenameWithoutExtension)
End Function
Sub Main()
Console.WriteLine(RemoveFileExtension("document.pdf"))
Console.WriteLine(RemoveFileExtension("archive.tar.gz"))
Console.WriteLine(RemoveFileExtension("README"))
Console.WriteLine(RemoveFileExtension("C:\Temp\file.txt"))
End Sub
End Module
' run:
'
' document
' archive.tar
' README
' C:\Temp\file
'