Visual Basic 6: Get last backslash

11 September 2009

Here is a function to find the last part of the string after the last backslash.

This is especially useful when dealing with filepaths and filenames.

Public Function GetLastBackSlash(Text As String) As String
    On Error GoTo TrapIt
    Dim i, pos As Integer
    Dim lastslash As Integer
    Text = Replace(Text, "/", "")
    For i = 1 To Len(Text)
        pos = InStr(i, Text, "", vbTextCompare)
        If pos <> 0 Then lastslash = pos
    Next i
    GetLastBackSlash = Right(Text, Len(Text) - lastslash)
TrapIt:
End Function

http://www.peternichol.com/entry/trackback/150/

Please leave a comment using the form provided.