GET filename directly from path

rickyfong

Registered User.
Local time
Today, 10:32
Joined
Nov 25, 2010
Messages
199
For instance, I got some dynamic length path with filename as follows:

F:\OUTDOOR\9-8\W1250832 ABC\working 1.3-0.6 BOARD 720.tif

Is there any easy way to get only the "working 1.3-0.6 BOARD 720.tif", which is the desired filename. Thanks a lot!!
 
Here you go, a procedure I've had kicking around for a little while, since I need to use it all the time.

Code:
Public Function GetFileName(ByVal FullPath As String) As String
'**************************************************
'*  Created By:     Scott L. Prince
'*  Created On:     10/2/13
'*  Modified:
'*  Purpose:        Returns a file name from the full path provided.
'*  Parameters:     Full path including file name
'*  Output:         File name, or empty string if no file name could be determined.
'*  Comments:
'**************************************************
On Error GoTo GetFileName_Err
 
    'Only necessary if a FullPath has actually been passed.
    If FullPath <> "" Then
        Dim BackslashLocation As Long   'Location of the last "/" or "\" in the path
        'Locate the FINAL backslash.
        BackslashLocation = InStrRev(FullPath, "\")
 
        'If no "\" was found, then check for "/" (sharepoint file structure).
        If BackslashLocation = 0 Then BackslashLocation = InStrRev(FullPath, "/")
 
        'Determine if a slash was found.
        If BackslashLocation > 0 Then
 
            'A slash was found, so return the file name.
            GetFileName = Right(FullPath, Len(FullPath) - BackslashLocation)
        Else
 
            'No slash found, so return FullPath as the file name.
            GetFileName = FullPath
        End If
    End If
 
GetFileName_Exit:
    Exit Function
 
GetFileName_Err:
    MsgBox "An error has occurred in procedure 'GetFileName'!" & vbCrLf & vbCrLf & _
           "Error:" & vbTab & vbTab & Err.Number & vbCrLf & _
           "Description:" & vbTab & Err.Description, vbOKOnly + vbCritical
    Resume GetFileName_Exit
End Function
 
Folder:
Mid("YourPath\Yourfilename.pdf", 1, instrrev("YourPath\Yourfilename.pdf", "\", -1)-1)
Name:
Mid("YourPath\Yourfilename.pdf", instrrev("YourPath\Yourfilename.pdf", "\", -1) + 1)

Or frothing's function
 

Users who are viewing this thread

Back
Top Bottom