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

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

**Excel:** `=TEXTAFTER(A2,"\",-1)` (Microsoft 365 only)

**Google Sheets:** `=REGEXEXTRACT(A2,"[^\\]+$")`

## Example

Sample data (row 1 is the header):

| A | B |
| --- | --- |
| File path | File name |
| C:\Users\ana\Reports\q3-summary.xlsx |  |
| D:\Projects\Atlas\Budget 2026.xlsx |  |
| \\fileserver\finance\invoices\INV-1042.pdf |  |
| C:\Temp\notes.txt |  |
| E:\Photos\2026\06\IMG_4471.jpg |  |
| C:\Users\ana\Desktop\readme |  |

Formula in B2, filled to B7: `=TEXTAFTER(A2,"\",-1)`

Result: q3-summary.xlsx; Budget 2026.xlsx; INV-1042.pdf; notes.txt; IMG_4471.jpg; readme

## 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.

## Other ways

- `=MID(A2,FIND("*",SUBSTITUTE(A2,"\","*",LEN(A2)-LEN(SUBSTITUTE(A2,"\",""))))+1,LEN(A2))`: 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.

## 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))`.

Every formula and result on this page was checked by running it in the Spreadsheet Practice formula engine on 2026-10-30.

Source: https://spreadsheetpractice.com/formulas/extract-file-name-from-path
