Parsing out a filename from a string

KGee

Well-known Member
Joined
Nov 26, 2008
Messages
537
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

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Never mind, I figure out I could use Right to give me what I needed.
 
Upvote 0
You could use MID and eliminate a few steps:

var4 = Mid(varName, InStrRev(varName, "\") + 1)
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,215,548
Messages
6,125,468
Members
449,230
Latest member
ASBeard

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
Back
Top