Extracting filename in a path

decook

New Member
Joined
Nov 18, 2003
Messages
4
We have over 500,000 file paths in a field. We need to extract just the file name (everything after the last"\") Can anyone think of a function we could use?
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I don't know your experience with VBA, but this function should do the trick:
Code:
Public Function fGet_Filename(strFilename As String) As String
Dim numLength As Integer
Dim i As Integer
numLength = 0
For i = 1 To Len(strFilename)
If Mid(strFilename, i, 1) = "\" Then
numLength = i
Else: numLength = numLength
End If
Next i
fGet_Filename = Right(strFilename, (Len(strFilename) - numLength))
End Function

You would call this code from a query. In your query's heading, write in something like:
Code:
Filename: fGet_Filename([Your_Filepath_Field])
of course, filling in 'your_filepath_field' with the field's name. The only problem with this function is that it will break if you have Null values in this field. To accomodate for Nulls, you'll need to handle it on your query side:
Code:
Filename: Iif([Your_Filepath_Field] Is Null,'No Filepath Given',fGet_Filename([Your_Filepath_Field]))
 
Upvote 0
These functions should be a bit more efficient (faster). If you have Access 2000 or above, then the second one is the best bet:
Code:
Function GetFileFromPath(strFileName As String) As String
    Dim intPos As Integer
    Do While InStr(intPos + 1, strFileName, "\") > 0
        intPos = InStr(intPos + 1, strFileName, "\")
    Loop
    GetFileFromPath = Mid(strFileName, intPos + 1)
End Function
Code:
Function GetFileFromPath2K(strFileName As String) As String
    GetFileFromPath2K = Mid(strFileName, InStrRev(strFileName, "\") + 1)
End Function

Call them as explained above.

Hope this helps,

Russell
 
Upvote 0
Thank you both. Very helpful. The first one worked like a charm...and my user is v ery happy :biggrin: thanks to you (y)

Kent
 
Upvote 0

Forum statistics

Threads
1,221,579
Messages
6,160,615
Members
451,658
Latest member
NghiVmexgdhh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top