473,473 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 12455
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 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
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...
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...
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.