Even faster ways to Extract file name from path [quick tip]

Share

Facebook
Twitter
LinkedIn

The best thing about Excel is that you can do the same thing in several ways. Our yesterdays problem – Extracting file name from full path is no different. There are many different ways to do it, apart from writing a formula. Learn these techniques to be a data extraction ninja.

1. Using Find Replace

Suggested by Iain in the comments yesterday, I love this technique for its simplicity and awesomeness.

  1. Select all the file paths
  2. Press CTRL+H
  3. Type *\ in find field
  4. Leave the replace field empty.
  5. Click on Replace all.
  6. Done!

It is that simple. Do not believe me? See this demo.

Extract file name from full path using find replace - Excel tip

Thanks Iain for teaching us this trick.

2. Using Text to columns utility

Buried inside heap of features in Excel is this beautiful Text to columns utility, that can take any text and convert it in to many columns based on the delimiter you specify. [more uses of text to columns]

This is how we can use it:

  1. Select all the file path cells
  2. Go to Data > Text to columns
  3. Chose “Delimited” in step 1 and click next.
  4. Specify delimiter as \Text to columns settings for extracting file name from full path - Excel
  5. Click Finish
  6. You will get all folders in to separate cells and file name in last cell.
  7. Now use a formula like =INDEX($C3:$O3,COUNTA($C3:$O3)) to extract the last cell’s value ie file name
  8. Done!

Extracting file name from path using text to columns utility and formulas - how to?

3. Using UDFs

While our formula method tends to be very long or very complicated, we can use 1-2 line VBA to get the file name from a full path. There are many ways to skin this cat in VBA, but 2 easiest methods are,

For both methods below, you first need to insert a new module and add the code in that.

Using InStrRev

As suggested by Daniel Ferry in the comments.

Public Function ParseFile(sPath As String) As Variant
ParseFile = Array(Mid$(sPath, 1 + InStrRev(sPath, “\”)), Mid$(sPath, 1 + InStrRev(sPath, “.”)))
End Function

Note: this UDF returns an array for file name & extension. So you need to enter it in 2 cells together.

The InStrRev() built in function searches for \ in the sPath from end and returns the first occurrence’s position.

Using split

As suggested by PPH in comments,

Function ExtractFileName(filepath) As String

Dim x As Variant
x = Split(filepath, Application.PathSeparator)
ExtractFileName = x(UBound(x))

End Function

What is your favorite method?

For most of my data cleaning needs, I use a mix of text to columns, find-replace or VBA. In rare cases, I rely on a formula. This is because data cleaning or extraction is usually one time step and figuring out a complex formula is not good idea in such cases.

What about you? How do you go about extracting filenames, dates, numbers etc. buried in text? What method do you use often? Please share with us in comments.

More tips on Data Extraction:

Facebook
Twitter
LinkedIn

Share this tip with your colleagues

Excel and Power BI tips - Chandoo.org Newsletter

Get FREE Excel + Power BI Tips

Simple, fun and useful emails, once per week.

Learn & be awesome.

Welcome to Chandoo.org

Thank you so much for visiting. My aim is to make you awesome in Excel & Power BI. I do this by sharing videos, tips, examples and downloads on this website. There are more than 1,000 pages with all things Excel, Power BI, Dashboards & VBA here. Go ahead and spend few minutes to be AWESOME.

Read my storyFREE Excel tips book

Overall I learned a lot and I thought you did a great job of explaining how to do things. This will definitely elevate my reporting in the future.
Rebekah S
Reporting Analyst
Excel formula list - 100+ examples and howto guide for you

From simple to complex, there is a formula for every occasion. Check out the list now.

Calendars, invoices, trackers and much more. All free, fun and fantastic.

Advanced Pivot Table tricks

Power Query, Data model, DAX, Filters, Slicers, Conditional formats and beautiful charts. It's all here.

Still on fence about Power BI? In this getting started guide, learn what is Power BI, how to get it and how to create your first report from scratch.

2 Responses to “Tour de France – Distance & Pace over time – Radial Charts”

  1. Kuldeep says:

    Simply Awesome....

  2. Asher says:

    The idea is awesome. Please can you help me to use this approach to build time spent duration in minutes.

Leave a Reply