Written by Allen Wyatt (last updated September 13, 2025)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365
Barry has a worksheet in which a column contains many file paths. He would like a way to extract just the filename (the part to the right of the final backslash) from each path. He wonders if there is a quick way to do this without using the Text to Columns feature.
There are several different ways, depending on whether you want to use a macro or not.
If your filenames are all the same length, then you can simply use the RIGHT function to pull out the last characters. (This formula assumes the full path and file name is in cell A1.)
=RIGHT(A1,11)
This assumes that the filename is always 11 characters long, such as "text001.txt". If the filename is a different length in each instance, then this approach won't work. Instead, you can try this formula:
=MID(A1,FIND(CHAR(1),SUBSTITUTE(A1,"\",CHAR(1), LEN(A1)-LEN(SUBSTITUTE(A1,"\",""))))+1,LEN(A1))
Note that the formula uses the SUBSTITUTE function twice. In each case it replaces the backslashes (\) with something else. The first time it replaces all of them with an ASCII value of 1 and the second time it replaces them with nothing (an empty string) so that it can determine how many backslashes were in the original path. The MID function is used to locate (with the help of FIND and the SUBSTITUTE functions) the location of the last backslash in the path and return everything after that point.
A shorter formula can be used if you are sure that the filename will never be more than 99 characters long:
=TRIM(RIGHT(SUBSTITUTE(A2,"\",REPT(" ",100)),99))
This formula replaces all the backslashes with 100 spaces, grabs the right-most 99 characters from the resulting string (that would be the filename with a bunch of spaces in front of it) and then trims off all the spaces.
If you want to use a macro you can create a very short function that will pull apart a string (the full path, in this case) based upon delimiters:
Function GetFileName(File_Path) As String Dim Parts Parts = Split(File_Path, Application.PathSeparator) GetFileName = Parts(UBound(Parts)) End Function
Told you it was short! The function that does the heavy work is the Split function which pulls a string apart based upon a delimiter you specify and stuffs the parts into an array. In this example the Split function uses as a delimiter whatever path separator is appropriate for the system on which Excel is running.
The last element of the resulting array (determined with the UBound function) contains the portion of the original path that is to the right of the last path separator—the file name.
You can develop an even shorter function to do the job:
Function GetFileName(File_Path) As String GetFileName=Mid(File_Path,InStrRev(File_Path,"\")+1 End Function
To use either function, put a formula like this in a cell:
=GetFileName(A1)
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12903) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Extracting File Names from a Path.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Need to figure out how many rows in a worksheet meet two criteria that you specify? Here's how to get the info you desire.
Discover MoreReplacing one character in a text value with another character is easy. All you need to do is use the SUBSTITUTE ...
Discover MoreIf you want to figure out the sum of all cells that contain formulas, there are a couple of ways you can go about it. ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2025-09-13 17:44:37
Dave
Missing right paren after +1 as below
Function GetFileName(File_Path) As String
GetFileName=Mid(File_Path,InStrRev(File_Path,"\")+1 )
End Function
2025-09-13 15:09:04
J. Woolley
Andy proposes this useful Excel 2024/365 formula assuming Barry's file paths are in column A beginning at row 2:
=TEXTAFTER(A2, "\", -1)
If you have an older version of Excel, My Excel Toolbox includes the following function:
=FindRev(FindText, WithinText, [StartNum], [CaseSensitive])
With this function, the equivalent of Andy's formula is
=MID(A2, FindRev("\", A2) + 1, 9999)
That formula is similar to the Tip's second GetFileName function (which needs to add a final closing parenthesis). Notice Excel's MID function requires the third argument, but VBA's Mid function does not.
My Excel Toolbox also includes these dynamic array functions:
=SplitText(Text, [Delimiter], [CaseSensitive], [Limit], [Remainder])
=ArraySize(Var)
=GetCols(RangeArray, ParamArray Cols())
Using these functions, the following formula is similar to the Tip's first GetFileName function:
=GetCols(SplitText(A2, "\"), ArraySize(SplitText(A2, "\")))
Here's a simpler version using Excel 2021's LET function:
=LET(x, SplitText(A2, "\"), GetCols(x, ArraySize(x)))
Finally, My Excel Toolbox includes the following regular expression function that is similar to Excel 365's REGEXEXTRACT:
=RegExMatch(Text, Pattern, [Mode], [IgnoreCase], [Multiline])
With this function, the following formula will return the filename from a file path:
=RegExMatch(A2, "[^\\/]+$")
The regular expression pattern "[^\\/]+$" works like this:
[^\\/] matches any character that is not backslash (\) or slash (/)
+ matches one or more occurrences of the preceding character set
$ requires the match to be at the end of the string
See https://sites.google.com/view/MyExcelToolbox/
2025-09-13 05:48:35
Andy
The new way, works in Microsoft 365:
=TEXTAFTER(A2,"\",-1)
You can also easily remove the file extension if needed:
=TEXTBEFORE(TEXTAFTER(G2,"\",-1),".",-1)
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments