Filename from Path

By | 2019-11-29

Given a fully qualified path and filename, returns only the filename.

Module

Function GetFileName(fname As String) As String
    Dim i As Long

    On Error Resume Next
    For i = Len(fname) To 1 Step -1
        If Mid(fname, i, 1) = "\" Then
            Exit For
        End If
    Next i
    GetFileName = Trim(Mid(fname, i + 1))
End Function

Usage

Private Sub Command1_Click()
    ' Good for removing the path
    MsgBox GetFileName("C:\Windows\System\moricons.dll")
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.