Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm trying to get the portion of a folder name that comes after the last "\". For instance.

 

k:\cortland\wade = wade

k:\cortland\wade\arch = arch

 

I've been trying to use a combination of Split and LastIndexOf but I'm not having any luck. Can anyone steer me in the right direction?

Posted
' if path is a string object containing the full path then
path.Substring(path.LastIndexOf("\") + 1)

Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted

There is already a function that exists specifically for this purpose (and is independant of environment, meaning it will work on a platform where the path separator character is / instead of \).

Dim FolderPath As String = "C:\\Documents And Settings\\MooCow"
Dim SubFolderName As String = System.IO.Path.GetFileName(FolderPath)

'Shows "MooCow"
MessageBox.Show(SubFolderName)

I know the function's name is GetFileName, but syntactically a folder path and an extensionless file name are the same, with one exception. A folder path can be followed by a trailing backslash, which should be taken into consideration.

[sIGPIC]e[/sIGPIC]
Posted

Yes, because in VB

 


dim FilePathOne as String = "C:\\MyDocuments\\Afolder\\somefile.txt"
dim FilePathTwo as String = "C:/MyDocuments\\Afolder/somefile.txt"

FilePathOne = FilePathTwo 'at least as far as System.IO is concerned

~Nate�

___________________________________________

Please use the [vb]/[cs] tags on posted code.

Please post solutions you find somewhere else.

Follow me on Twitter here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...