Lost in String Manipulation

bjwade62

Centurion
Joined
Oct 31, 2003
Messages
104
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?
 
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 \).
Visual Basic:
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.
 
Yes, because in VB

Visual Basic:
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
 
Back
Top