If you need to know a files name from a file path it might be interesting to also know, if this file is alos existing.

Below you will find a method, which only returns a files name if the file is available in current file system.

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
' @Author - Alexander Bolte
' @ChangeDate - 2014-10-21
' @Description - Returning a files name from a given file path.
' @Param filePath - a String providing a full path to one file.
' @Returns - a String holding a files name, if file exists and is accessible. Else an empty String is returned.
Public Function getFileNameFromFilePath(ByVal filePath As String) As String
    Dim fileName As String
    Dim fso As New scripting.FileSystemObject
    Dim f As scripting.File
    
    On Error GoTo errHandle:
    
    If fso.fileExists(filePath) Then
        Set f = fso.GetFile(filePath)
        fileName = f.Name
    End If
    Set f = Nothing
    Set fso = Nothing
    
errHandle:
    If Err.Number <> 0 Then
        Err.Clear
    End If
    
    getFileNameFromFilePath = fileName
End Function

Referenced API

Referenced APIs can be found below.

Mircosoft Scripting Runtime