Searching backwards through string.

revver

Active Member
Joined
Dec 20, 2007
Messages
257
I have a column (D) of fully qualified file names eg

D:\folder\another folder\bigfolder\filename.bmp
D:\largefolder\morefolder\folder\another folder\nextfolder\smallfile.jpg

I want to extract just the filename to column G.
To do this I expect I will need to search backwards through the filename string looking for the final "\"
From that I can just slice off the filename I want.

Is there a nest of intrinsic functions I can use or is it easier to create a user defined function?
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Give this formula a try...

=TRIM(RIGHT(SUBSTITUTE(D1,"\",REPT(" ",260)),260))
 
Upvote 0
Thanks Rick. It works a treat.
Now I have to figure how it works.
I know TRIM and RIGHT but I'll have to hit the books for REPT and SUBSTITUTE
Thanks again.
 
Upvote 0
Now I have to figure how it works.
The formula replaces each backslash with 260 spaces (where 260 is the maximum length of a path in Windows). What that means is the filename (the part you want) is separated from the rest of the text by 260 spaces. Now we take the right 260 characters from the end... in there is the complete filename plus some leading blank spaces, so we apply the TRIM function to get rid of them.
 
Upvote 0
Yep. Figured it out but not the reason for using 260. Suspected it had to be to do with path length but I thought that was 255. Now I know. Thanks.
 
Upvote 0

Forum statistics

Threads
1,207,095
Messages
6,076,551
Members
446,213
Latest member
bettigb

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