Parsing out a filename from a string

KGee

Well-known Member
Joined
Nov 26, 2008
Messages
532
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have a list of UNC paths where I want to parse out only the name of the file and I'm having some difficulty. For example, using any of the scenarios below I want to extract "FILENAME.XLS" from the string. The file name and extension will vary in addition to the name and lengths of the folder paths.

varName = "\\server_name\share_name\folder1_name\FILENAME.XLS"
varName = "\\server_name\share_name\folder1_name\folder2_name\FILENAME.XLS"
varName = "\\server_name\share_name\folder1_name\folder2_name\folder3_name\FILENAME.XLS"

I used InStrRev to determine the position of the last \, Len to get the length of the string and subtracted the second from the first to get the file length.
Code:
var1 = Len(varName)
var2 = InStrRev(varName, "\")
var3 = (var1 - var2)
I assume that's what I needed to do, but how do I go about the next step and return the file name as a string to var4?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.

KGee

Well-known Member
Joined
Nov 26, 2008
Messages
532
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Never mind, I figure out I could use Right to give me what I needed.
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
You could use MID and eliminate a few steps:

var4 = Mid(varName, InStrRev(varName, "\") + 1)
 

KGee

Well-known Member
Joined
Nov 26, 2008
Messages
532
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
You could use MID and eliminate a few steps:

var4 = Mid(varName, InStrRev(varName, "\") + 1)
Thanks! I was able to get it down to two lines but this is even better.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,302
Messages
5,836,512
Members
430,436
Latest member
fefenouil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top