Excel Formula Extract path from file name
If you want to extract the path from full path and file name, you can use a formula which combines the LEFT, FIND, SUBSTITUTE and LEN functions to handle it. The formula is a little long, but this tutorial will explain how the formula works for you.
Generic formula:
LEFT(path,FIND("?",SUBSTITUTE(path,"\","?",LEN(path)-LEN(SUBSTITUTE(path,"\",""))))) |
Arguments
Path: the cell reference or text string contains file path and file name. |
How this formula work
Take an instance: to extract the path from cell B3, which contains full path and the file name, please use below formula:
=LEFT(B3,FIND("?",SUBSTITUTE(B3,"\","?",LEN(B3)-LEN(SUBSTITUTE(B3,"\",""))))) |
Press Enter key to extract the path from the cell.
Explanation
To extract the path from the full path and file name, firstly, the formula counts the number of character “\” by the LEN and SUBSTITUTE functions, then replace the last “\” with a special character “?” by the SUBSTITUTE function, finally, find the special character “?” and extract the path by using the FIND and LEFT functions.
LEN function returns the number of characters in the text string.
SUBSTITUTE function replaces old text with a new one.
The formula LEN(B3)-LEN(SUBSTITUTE(B3,"\","")) counts the number of characters "\".
=LEN(B3)-LEN(SUBSTITUTE(B3,"\","")) =LEN(B3)-LEN(“C:UsersAddinTestWin10Documentsdescription.xlsx”) 50-46 =4 |
SUBSTITUTE(B3,"\","?",LEN(B3)-LEN(SUBSTITUTE(B3,"\",""))) replaces the fourth "\" with "?".
=SUBSTITUTE(B3,"\","?",LEN(B3)-LEN(SUBSTITUTE(B3,"\",""))) =SUBSTITUTE(B3,"\","?",4) =C:\Users\AddinTestWin10\Documents?description.xlsx |
FIND function is used to find a string within another string, and returns the starting position of the string inside the other one.
The formula FIND("?",SUBSTITUTE(B3,"\","?",LEN(B3)-LEN(SUBSTITUTE(B3,"\","")))) finds the position of “?” in the text string "C:\Users\AddinTestWin10\Documents?description.xlsx".
=FIND("?",SUBSTITUTE(B3,"\","?",LEN(B3)-LEN(SUBSTITUTE(B3,"\","")))) =FIND("?","C:\Users\AddinTestWin10\Documents?description.xlsx") =34 |
LEFT function extracts substring with fixed length from left side of the given text.
=LEFT(B3,FIND("?",SUBSTITUTE(B3,"\","?",LEN(B3)-LEN(SUBSTITUTE(B3,"\",""))))) =LEFT(B3,34) =C:\Users\AddinTestWin10\Documents\ |
Sample File
Relative Formulas
- Extract from right until character
Here introduces the formula to extract text from right side of a given text until a specified character. - Extract extension from file name
Here introduces the formula to extract the file extension from file name to another column. - Extract file name from path
lt explains how to apply formulas to extract filename with or without extension from a file path in a specific cell in Excel. - Add dashes to phone number
To add dashes to phone number, you can use a formula to solve it in Excel.
- LEFT function
Extract substring from left side of text. - SUBSTITUE function
Find and replace text string with another one. - LEN function
Get the number of characters in the text. - SEARCH function
Find the position of a specific character or substring from the given text string. - FIND function
Find a string within another string
The Best Office Productivity Tools
Kutools for Excel - Helps You To Stand Out From Crowd
Kutools for Excel Boasts Over 300 Features, Ensuring That What You Need is Just A Click Away...
Office Tab - Enable Tabbed Reading and Editing in Microsoft Office (include Excel)
- One second to switch between dozens of open documents!
- Reduce hundreds of mouse clicks for you every day, say goodbye to mouse hand.
- Increases your productivity by 50% when viewing and editing multiple documents.
- Brings Efficient Tabs to Office (include Excel), Just Like Chrome, Edge and Firefox.