Remove path from file name

dantb

Active Member
Joined
Mar 20, 2002
Messages
358
Hello all: Is there a formula where only the data after the second or third \ be shown in a cell no mater what the path is? Thanks Dan

D:\A Print\D72PI010-R00.PDF
D:\A Print\Temp\D72PI010-R00.PDF

Formula in both cases would return just the file name D72PI010-R00.PDF
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Needs a custom function. Alt +F11 then Copy paste this to a code module.
Usage the same as other functions
=GetFileName(A1)
Code:
Function GetFileName(nm As String)
    L = Len(nm)
    C = L
    While Mid(nm, C, 1) <> "\"
        C = C - 1
    Wend
    GetFileName = Right(nm, L - C)
End Function
 
Upvote 0
Works great Brian! Played with Len & Right but could never put nothing together that worked. Thanks again for your help. Dan
 
Upvote 0
dim Files
dim FileName as string

Files = split(path,"\")
FileName = Files(ubound(files))

This way doesnt loop around but is not better then the abouve just another approach.
 
Upvote 0
Thanks Bandit: Will remember both ways. Thank both of you for all your help. Dan
 
Upvote 0
=REPLACE(A1,1,FIND("@",SUBSTITUTE(A1,"\","@",LEN(A1)-LEN(SUBSTITUTE(A1,"\","")))),"")
 
Upvote 0
Thanks Fairwinds: Works well also, Im not sure what the (@) symble is doing but the formula does work, Thanks Dan
 
Upvote 0
The @ sign is replacing the last instance of / in the full file name, afterwhich it is used as the search criteria to the FIND function.
 
Upvote 0
Thanks Jon: I never have used the ( @ ) symble like that before, will have to play with that one. Thanks again Dan
 
Upvote 0
dantb said:
Thanks Jon: I never have used the ( @ ) symble like that before, will have to play with that one. Thanks again Dan

The @ symbol could have been any unlikely-to-be-found character -- the intent here is to SUBSTITUTE the right-most slash with a unique character on which to key a search.
 
Upvote 0

Forum statistics

Threads
1,219,162
Messages
6,146,660
Members
450,706
Latest member
LGVBPP

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