Jen53403
Programmer
- Jul 17, 2006
- 22
I want to strip the filename off of a path like so:
c:\directory\path\filename.ext
becomes
c:\directory\path\
I couldn't find a premade function in VB to do something like this, so I'm trying to write my own function below. It searches the string backwards for the first instance of the backslash and returns the substring before that index. The problem is, it isn't working! Please, someone find the stupid mistake in this code!
c:\directory\path\filename.ext
becomes
c:\directory\path\
I couldn't find a premade function in VB to do something like this, so I'm trying to write my own function below. It searches the string backwards for the first instance of the backslash and returns the substring before that index. The problem is, it isn't working! Please, someone find the stupid mistake in this code!
Code:
Private Function GetDir(ByVal path As String) As String
'Removes the file name portion of a path string
Dim i As Integer
Dim chars As Char() = CType(path, Char())
i = Len(path) - 1
Do While i > 0 And chars(i) <> "\92"
i = i - 1
Loop
Dim retStr As String
retStr = Mid(path, 1, Len(path) - i)
Return retStr
End Function