how to extract file name and path separately

S

steve

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
 
I

Imran Koradia

dim sFile as String = "c:\mydir\subdir\temp\myfile.txt"
dim oFileInfo as New System.IO.FileInfo(sFile)
debug.WriteLine(oFileInfo.DirectoryName)
debug.WriteLine(oFileInfo.Name)

hope this helps..
Imran.
 
J

Jeff Johnson [MVP: VB]

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

In addition to Imran's response, check out the System.IO.Path class.
 
K

Kevin Hodgson

Easiest way is to use a FileInfo object, part of System.IO

Dim filePath As String = "c:\mydir\subdir\temp\myfile.txt"
Dim checkFileInfo As New System.IO.FileInfo(filePath)

Dim path as String = checkFileInfo.DirectoryName
Dim fileName as String = checkFileInfo.Name
 
S

Steve Long

I'm not sure which is faster but:
ipos = filepathAndName.LastIndexOf("\")
path = filepathAndName.Substring(0, ipos)
filename = filepathAndName.Substring(ipos + 1, filepathAndName.Length -
ipos - 1)

Steve
hope my math was right
 
H

Herfried K. Wagner [MVP]

* "steve said:
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

'System.IO.Path' -> Press F1 key.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top