CodeItBetter Programming Another VB Programming Blog

How to Retrieve the File Name from a Path

Posted on January 5, 2009
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
'File/Folder Handling - How to Retrieve the File Name from a Path
Option Explicit
 
Public Function GetFileName(FilePath As String) As String
    Dim slashLocation As Integer
 
    On Error GoTo GetFileName_Error
 
    If Len(FilePath) = 0 Then
        GetFileName = ""
        Exit Function
    End If
 
    'Find the last \ in the path
    slashLocation = InStrRev(FilePath, "\")
 
    GetFileName = Mid$(FilePath, slashLocation + 1, Len(FilePath) + 1 - slashLocation)
 
ExitHere:
    On Error GoTo 0
    Exit Function
 
GetFileName_Error:
    GetFileName = ""
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetFileName of Form Form1"
End Function
Comments (1) Trackbacks (0)
  1. AWESOME! Thanks!!! :) CodeItBetter is the perfect name after reading this post


Leave a comment


 

No trackbacks yet.