469,275 Members | 1,841 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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 12256
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by mtech1 | last post: by
5 posts views Thread by Kristoffer Persson | last post: by
9 posts views Thread by Scott Reynolds | last post: by
3 posts views Thread by SteveB | last post: by
5 posts views Thread by Steve | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.