Visual Basic 6 function "basename"

Go back

Below you'll find the source for the Visual Basic 6 function basename.

Attribute VB_Name = "modBasename"
' This function is downloaded from:
' You may freely distribute this file but please leave all comments, including this one, in it.
' @Author Stefan Thoolen <>

Option Explicit

' Returns filename component of path
' Same syntax as the PHP function 'basename'
' See also:
' @param    Sting   Path        The full path
' @param    Sting   suffix      Optional, a string that must be removed if it exists as suffix
' @return   String              The base filename
' @author   Stefan Thoolen <>
Public Function basename(Path As String, Optional suffix As String) As String
    Dim i As Integer
    i = InStrRev(Replace(Path, "\", "/"), "/")
    basename = Right(Path, Len(Path) - i)
    If LCase(Right(basename, Len(suffix))) = LCase(suffix) Then basename = Left(basename, Len(basename) - Len(suffix))
End Function