Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Extracting File Names from a Path.

Extracting File Names from a Path

Written by Allen Wyatt (last updated June 13, 2023)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12903) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Changing ToolTips for a Macro Button

Want to change the ToolTip that appears when you hover the mouse pointer over a tool on the Quick Access Toolbar? Here's ...

Discover More

Using the Format Painter with Editing Restrictions in Place

Word allows you to apply protection to your documents that can affect which tools users can access. If you want to exempt ...

Discover More

Adding Hyperlinks to an Index

Word provides a full set of tools you can use to generate an index for your document. One thing you can't do, however, is ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Alphabetic Column Designation

Want to know the letters assigned by Excel to a particular column? Excel normally deals with column numbers, but you can ...

Discover More

Combining Cell Contents

Excel allows you to easily combine text together. Interestingly, it provides two ways you can perform such combinations. ...

Discover More

Inserting the Workbook Name

Do you want to insert the name of your Excel workbook into a cell? There are a number of ways you can do this, as ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 4 + 9?

2019-10-16 08:25:01

JC

I recommend investigating the use of FileSystemObject, which can easily parse a fully-qualified filename to provide:
file name
extension
base name
parent folder
absolute path
...and more


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.