Spreadsheet Practice

How to extract the file name from a file path in Excel

Excel formula

=TEXTAFTER(A2,"\",-1)

Take the text after the last backslash: =TEXTAFTER(A2,"\",-1) in Excel 365.

Result on the example
one result per row in B2, filled down to B7
Works in
Microsoft 365 only
Google Sheets
Different formula, see below

See it work

The formula is already in B2, filled down to B7. Change any input and the result updates, or edit the formula to experiment.

How it works

The instance number -1 tells TEXTAFTER to use the last backslash, however many folders the path has. Everything after it is the file name.

In Google Sheets, [^\]+$ matches the characters at the end of the text that aren't a backslash.

=TEXTAFTER(A2,"\",-1)

Result"q3-summary.xlsx"

  1. A2"C:\Users\ana\Reports\q3-summary.xlsx"

    The value in A2.

  2. -1-1

    Makes the value negative.

  3. TEXTAFTER(A2,"\",-1)"q3-summary.xlsx"

    TEXTAFTER: Text after a delimiter.

    • textA2"C:\Users\ana\Reports\q3-summary.xlsx"
    • delimiter"\"
    • instance_num-1-1

In Google Sheets

Google Sheets needs a different formula: =REGEXEXTRACT(A2,"[^\\]+$"). On the same data it gives the same result. Here it is running in Google Sheets mode.

Other ways to do it

Each one was run on the same example sheet.

FormulaResultWhen to use it
=MID(A2,FIND("*",SUBSTITUTE(A2,"\","*",LEN(A2)-LEN(SUBSTITUTE(A2,"\",""))))+1,LEN(A2))q3-summary.xlsx, Budget 2026.xlsx, INV-1042.pdf…Works in every version. LEN(A2)-LEN(SUBSTITUTE(A2,"\","")) counts the backslashes, SUBSTITUTE marks the last one with *, which can't appear in a Windows path, and MID returns what follows.

Common mistakes

  • Web addresses and Mac paths use forward slashes. Use "/" instead, or both: =TEXTAFTER(A2,{"\","/"},-1).
  • A folder path that ends with a backslash returns an empty result, because nothing follows the last one.

Try it yourself

Same data, empty answer cell. Your formula is graded instantly, then re-checked on changed data, so a typed-in number won't pass.

Extract the file name from a file path

In B2, extract the file name (everything after the last backslash) from the path and fill down to B7.

Questions

How do I get the folder path without the file name?

Take what comes before the last backslash: =TEXTBEFORE(A2,"\",-1).

How do I get the file name without its extension?

Cut at the last dot too: =TEXTBEFORE(TEXTAFTER(A2,"\",-1),".",-1,,,TEXTAFTER(A2,"\",-1)).

Checked by running on 2026-10-30: every formula and result on this page was calculated by the Spreadsheet Practice formula engine, in both Excel and Google Sheets modes.