Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse file name out of path 5

Status
Not open for further replies.

roswald

Programmer
Jul 6, 2002
152
US
My dear friends I can do this in a few other languages with no problem but doing it in VB has me stumped.
How can I get 'MyFile' out of a string that has filename and path? i.e. 'J:\downld\fldr\another\MyFile.txt'

I am really stumped, please show me the way!

Thanks for your help
 
Dim Test As String
Dim TestFileNamePos As Integer
Test = "J:\downld\fldr\another\MyFile.txt"
TestFileNamePos = InStrRev(Test, "\")
MsgBox Mid$(Test, TestFileNamePos + 1) Swi
 
Thanks for a better way... I've been doing it the hard way. Tuna - It's fat free until you add the Mayo!
 
Or even:
[tt]
Dim fso As Object
Set fso = CreateObject("scripting.filesystemobject")
MsgBox fso.GetFileName("J:\downld\fldr\another\MyFile.txt")
 
This is why I can't leave this place for more than 5 minutes... there's always more than 1 way to skin a filename... in all my years of coding, I've never been so happy with a resource as I am now.

thanks for two good solutions, both of which I never thought to use, because my way "worked" and i never needed to change it. Tuna - It's fat free until you add the Mayo!
 

How about a one liner?...
[tt]
Dim S As String
S = "J:\downld\fldr\another\MyFile.txt"
MsgBox Mid(S, InStrRev(S, "\") + 1)
[/tt]

This just combines what SWI does above into one line.

 
Thanks experts! All excellent code solutions. I appreciate your help.

Roswald
 
I had trouble using
fso.GetFileName "J:\downld\fldr\another\MyFile.txt")

because it only works if the file actually exists.

David
 
<bzzzt> wrong, I'm afraid. GetFileName doesn't care whether the file exists or not. I happen to know this, but just for the record here is part of the helpfile entry:
[tt]
The GetFileName method works only on the provided path string. It does not attempt to resolve the path, nor does it check for the existence of the specified path.

 
id like to star you all but vb5prgrmr has stole my vote...
 
How do you vote? This thread deserves recognition.
 
At the bottom left of each individual post is a shortcut (link) that says ' Mark this post as a helpful/expert post!'

Just click the link!
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
It's probably worth pointing out that the FSO solution deals with common typos (e.g. using / instead of \ as a path seperator) and with URLs. So:

MsgBox fso.GetFileName(&quot;J://downld\fldr/another\MyFile.txt&quot;)

works, as does

MsgBox fso.GetFileName(&quot;thread222-432765
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top