469,338 Members | 8,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

how to extract file name and path separately

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
5 21325
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

"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
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
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
* "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.

Similar topics

4 posts views Thread by mtech1 | last post: by
39 posts views Thread by Joe Laughlin | last post: by
5 posts views Thread by Kristoffer Persson | last post: by
3 posts views Thread by Bill | last post: by
1 post views Thread by Miki Peric | last post: by
4 posts views Thread by Alan T | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by haryvincent176 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.