By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

how to extract file name and path separately

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

"steve" <noemail.@try.com> wrote in message
news:_M**********************@wagner.videotron.net ...
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

Nov 21 '05 #2

P: n/a

"steve" <noemail.@try.com> wrote in message
news:_M**********************@wagner.videotron.net ...
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.
Nov 21 '05 #3

P: n/a
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

"steve" <noemail.@try.com> wrote in message
news:_M**********************@wagner.videotron.net ...
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

Nov 21 '05 #4

P: n/a
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" <noemail.@try.com> wrote in message
news:_M**********************@wagner.videotron.net ...
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

Nov 21 '05 #5

P: n/a
* "steve" <noemail.@try.com> scripsit:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.