← All how-to recipes

How to extract the file name from a path or URL

✓ Verified in LibreOffice 25.8.7.3 ✓ Verified in Google Sheets (2026-08-30)

Get just the file name (or last segment) from a path or URL — clean up import logs, links, and directory listings.

The formula

AppFormulaNotes
Excel (desktop)=TRIM(RIGHT(SUBSTITUTE(A2,"/",REPT(" ",200)),200))For Windows backslash paths, replace "/" with a backslash. Excel 365: =TEXTAFTER(A2,"/",-1).
Google Sheets=TRIM(RIGHT(SUBSTITUTE(A2,"/",REPT(" ",200)),200))Identical; or =REGEXEXTRACT(A2,"[^/]+$").
LibreOffice Calc=TRIM(RIGHT(SUBSTITUTE(A2,"/",REPT(" ",200)),200))Identical.

How it works

The last-word trick pointed at the path separator: SUBSTITUTE pads every "/" to 200 spaces so the final segment sits alone in the rightmost 200 characters, RIGHT grabs it, and TRIM cleans up — "docs/2026/reports/report.xlsx" yields "report.xlsx". Swap "/" for a backslash to handle Windows file paths. On modern versions TEXTAFTER(A2,"/",-1) does it directly (the -1 means after the LAST separator). To then drop the extension, apply the same idea to the "." with LEFT/FIND.

Verified, not just documented

We ran =TRIM(RIGHT(SUBSTITUTE(A2,"/",REPT(" ",200)),200)) in LibreOffice 25.8.7.3 (headless, with forced recalculation) and it returned report.xlsx — exactly the expected result. We then ran the same formulas in Google Sheets, executed 2026-08-30: a formula-only workbook goes into Google Drive, which converts it to a Sheet and recalculates every formula with Google’s own engine, and comes back out as .xlsx carrying the values Google computed. It returned report.xlsx for the worked example, the same value LibreOffice produced. Both engines’ numbers on this page are executed results. The Excel formula follows Microsoft’s official documented syntax — we do not run desktop Excel.

Functions used

TRIM · RIGHT · SUBSTITUTE · REPT — see full Excel, Google Sheets & LibreOffice compatibility for each.

Related recipes

Related comparisons