473,395 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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)
Nov 21 '05 #1
5 6580
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)

Nov 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)


Nov 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
Nov 21 '05 #4
Or you could use the shared methods of the Path class

Nov 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 23 '05 #6

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

Similar topics

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 "C:\A Long Time Ago\In A Galaxy\Far Far...
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 regardless of the size of it's name. For the above...
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 the file name from the full path, e.g....
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 Thanks Scott
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 = c:\mydir\subdir\temp filename = myfile.txt TIA
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 different Job Seekers. I have stored all the Word...
14
by: skyy | last post by:
hi... i got this filename test.txt with the location of c:\testfolder\test.txt i can get the full path name from the upload object. But i want to extract only the filename.(test.txt) i tried...
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 page with VB.Net that will populate a SQL...
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 are then placed on a server, but I need to...
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 "c:\temp.doc) and paste into it, but i only can work out how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.