472,341 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,341 software developers and data experts.

Extract path from path & filename

Hi,

Using the code below I can extract the filename from a path but I would like
to know how to get just the path too.
So if the full path is "C:\A Long Time Ago\In A Galaxy\Far Far
Away\SomeFilename.txt"
The filename is: SomeFilename.txt
The path only is: C:\A Long Time Ago\In A Galaxy\Far Far Away\

I know there's other methods of getting the filename only, path only etc.
that are new to VB.NET but they involve using the FileInfo/DirectoryInfo
classes and for this I prefer to use the InStrRev/Trim method.

Thanks,
Paul

Dim i As Integer
Dim sFilename As String
Dim file As String = "C:\A Long Time Ago\In A Galaxy\Far Far
Away\SomeFilename.txt"

i = InStrRev(file.Trim, "\")
If i = 0 Then
'No path found, only filename
sFilename = file.Trim
Else
'Assign filename only to variable
sFilename = Right(file.Trim, Len(file.Trim) - i)
End If

MsgBox(sFilename)
Jul 21 '05 #1
5 12377
You can use virtually the same code...just change to use the MID function,
retunring the 1st character through the character before the first \ from
the end.
'Assign path only to variable
sPath = Mid(file.Trim, 1, i-1)
"Paul" <me@home.com> wrote in message
news:g5********************@pipex.net... Hi,

Using the code below I can extract the filename from a path but I would
like to know how to get just the path too.
So if the full path is "C:\A Long Time Ago\In A Galaxy\Far Far
Away\SomeFilename.txt"
The filename is: SomeFilename.txt
The path only is: C:\A Long Time Ago\In A Galaxy\Far Far Away\

I know there's other methods of getting the filename only, path only etc.
that are new to VB.NET but they involve using the FileInfo/DirectoryInfo
classes and for this I prefer to use the InStrRev/Trim method.

Thanks,
Paul

Dim i As Integer
Dim sFilename As String
Dim file As String = "C:\A Long Time Ago\In A Galaxy\Far Far
Away\SomeFilename.txt"

i = InStrRev(file.Trim, "\")
If i = 0 Then
'No path found, only filename
sFilename = file.Trim
Else
'Assign filename only to variable
sFilename = Right(file.Trim, Len(file.Trim) - i)
End If

MsgBox(sFilename)

Jul 21 '05 #2
Thanks Lawrence, that did the job nicely.
"Lawrence J. Rizzo" <lj********@adelphia.net> wrote in message
news:Kr********************@adelphia.com...
You can use virtually the same code...just change to use the MID function,
retunring the 1st character through the character before the first \ from
the end.
'Assign path only to variable
sPath = Mid(file.Trim, 1, i-1)


"Paul" <me@home.com> wrote in message
news:g5********************@pipex.net...
Hi,

Using the code below I can extract the filename from a path but I would
like to know how to get just the path too.
So if the full path is "C:\A Long Time Ago\In A Galaxy\Far Far
Away\SomeFilename.txt"
The filename is: SomeFilename.txt
The path only is: C:\A Long Time Ago\In A Galaxy\Far Far Away\

I know there's other methods of getting the filename only, path only etc.
that are new to VB.NET but they involve using the FileInfo/DirectoryInfo
classes and for this I prefer to use the InStrRev/Trim method.

Thanks,
Paul

Dim i As Integer
Dim sFilename As String
Dim file As String = "C:\A Long Time Ago\In A Galaxy\Far Far
Away\SomeFilename.txt"

i = InStrRev(file.Trim, "\")
If i = 0 Then
'No path found, only filename
sFilename = file.Trim
Else
'Assign filename only to variable
sFilename = Right(file.Trim, Len(file.Trim) - i)
End If

MsgBox(sFilename)


Jul 21 '05 #3
Paul,

For that is the path class.
http://msdn.microsoft.com/library/de...mberstopic.asp

I give you the member page the class page is for this in my opinion a little
bit confusing.

I hope this helps,

Cor
Jul 21 '05 #4
Or you could use the shared methods of the Path class

Jul 21 '05 #5
Thanks Chris and Cor


"Cor Ligthert" <no************@planet.nl> wrote in message
news:Oe**************@TK2MSFTNGP12.phx.gbl...
Paul,

For that is the path class.
http://msdn.microsoft.com/library/de...mberstopic.asp

I give you the member page the class page is for this in my opinion a
little bit confusing.

I hope this helps,

Cor

Nov 4 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: mtech1 | last post by:
Access 2000 I need to extract just the filename from a path. Ex: path is- c:\MyApp\Sub\MyApp.exe I need to capture just the file name...
5
by: Kristoffer Persson | last post by:
It's probably very simple... I would like to know if there is a function similar to ExtractFileName() (I've used it in Delphi), which returns only...
9
by: Scott Reynolds | last post by:
Hello! Could someone please provide me a sample, how to extract filename from url? http://www.mydomain.com/eng/Default.aspx -> Default.aspx ...
5
by: steve | last post by:
How can someone extract the file name and file path from a complete path? ex: c:\mydir\subdir\temp\myfile.txt will give: path =...
5
by: Paul | last post by:
Hi, Using the code below I can extract the filename from a path but I would like to know how to get just the path too. So if the full path is...
0
by: savvy | last post by:
I am developing a Recruitment Agency Website I am using MS SQL Server in which I have a table called CV_Details which stores all the details of...
3
by: SteveB | last post by:
I have posted this question in the Visual Basic 2005 and Visual Basic .Net 2005 discussion groups, also. Hi. I am developing an application/web...
5
by: Steve | last post by:
Hi all Does anybody please know a way to extract an Image from a pdf file and save it as a TIFF? I have used a scanner to scan documents which...
6
by: Paul Mc | last post by:
Hi all, It's a little late in the day for me so please forgive as i need my bed.!! The issue is i need to open a word doc (say...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.