Ads 468x60px

Wednesday, October 24, 2012

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

Even faster ways to Extract file name from path [quick tip]:
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(filespath) 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:

0 comments:

Post a Comment