Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Goofy string manipulation in VB 1

Status
Not open for further replies.

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!

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
 
Code:
        Dim str As String = "c:\directory\path\filename.ext"
        Dim strNoFile As String = str.Substring(0, str.LastIndexOf("\") + 1)

Hope this helps,

Alex

[small]----signature below----[/small]
I'm pushing an elephant up the stairs

My Crummy Web Page
 
use the System.io.path clas it has all the methods you look for.


I don't know whether to laugh or cry. I've got a file class I created just to do those kinds of things and now I know they were there all a long. Laugh, it wasn't that hard to do. My class does other things too so it gets to stay, but I'll be reworking it now.

-I hate Microsoft!
-Forever and always forward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top