Function Get Filename from File Path


Function GetFilenameFromPath(ByVal strPath As String) As String
On Error GoTo errorhandler
If Right$(strPath, 1) <> “\” And Len(strPath) > 0 Then
GetFilenameFromPath = GetFilenameFromPath(Left$(strPath, Len(strPath) – 1)) + Right$(strPath, 1)
End If
Exit Function
End Function

How to Use It:

The above function will return a file name with its extension. It returns the rightmost characters of a string up to, but not including the rightmost ‘\’. You can call this function as:

Dim myPath as String

myPath = “C:\myFolder\MyDoc.doc”

call GetFilenameFromPath(myPath)

it will return “MyDoc.doc”

Related posts