how to extract file name and path separately

  • Thread starter Thread starter steve
  • Start date Start date
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
 
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.
 
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.
 
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
 
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
 
* "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.
 
Back
Top