Last Subfolder Name

sdear

Board Regular
Joined
May 23, 2010
Messages
96
Windows 10, Excel 365
I'm trying to get just the last subfolder name in a path:
C:\ABC\DEF\ABC.txt will give me ABC
I need: C:\ABC...\WTH\ABC.txt or
C:\DEF...\WTH1\ABC.txt
to give me WTH or WTH1.
I'm trying to get away from the 40 pound path: C:\ABC...\WTH\.
I'm trying to remove multiple files (same or similar names) from multiple folders. My macro gives me all the 40 pound names...
Thank you
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Try:

Book1
AB
1C:\ABC\DEF\ABC.txtDEF
2C:\ABC…\WTH\ABC.txtWTH
3C:\DEF…\WTH1\ABC.txtWTH1
4C:\ABC\something\else\DEF\Save files\myfile.pdfSave files
Sheet3
Cell Formulas
RangeFormula
B1:B4B1=TRIM(LEFT(RIGHT(SUBSTITUTE(A1,"\",REPT(" ",99)),198),99))
 
Upvote 0
Solution
For C:\ABC\DEF\GHI\JKL\ABC\SWF\ABC.txt this gives me DEF. I'm trying to get the last subfolder name (SWF).
 
Upvote 0
It works for me:

Book1
AB
1C:\ABC\DEF\ABC.txtDEF
2C:\ABC…\WTH\ABC.txtWTH
3C:\DEF…\WTH1\ABC.txtWTH1
4C:\ABC\something\else\DEF\Save files\myfile.pdfSave files
5C:\ABC\DEF\GHI\JKL\ABC\SWF\ABC.txtSWF
Sheet1
Cell Formulas
RangeFormula
B1:B5B1=TRIM(LEFT(RIGHT(SUBSTITUTE(A1,"\",REPT(" ",99)),198),99))


If it still doesn't work for you, try using the XL2BB tool (see the link in the reply box) to show what your sheet looks like. It may be that the slash character on your sheet is a different ASCII code that what my formula expects.
 
Upvote 0
I'm such an idiot: I looked at another worksheet in the file I'm working with and found I already had a macro that gives me what I want...
BTW: the ASCII for \ on my pc is 92
 
Upvote 0
My ASCII code for \ on my PC is also 92, so the formula should work for you. Regardless, it appears that you found a solution. ?
 
Upvote 0
Your formula works. I'm not sure what happened yesterday. I must have sent it looking in the wrong cell.
I'll mark your solution as THE solution.
Thank you for your help
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,696
Members
449,048
Latest member
81jamesacct

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