Visual Basic 6 function "pathinfo"

Go back

Below you'll find the source for the Visual Basic 6 function pathinfo.

Attribute VB_Name = "modPathinfo"
' This function is downloaded from:
' http://www.stefanthoolen.nl/archive/vb6-functions/
' 
' You may freely distribute this file but please leave all comments, including this one, in it.
' 
' @Author Stefan Thoolen <mail@stefanthoolen.nl>

Option Explicit

Public Type STRUCTURE_PATH_INFO
    dirname As String
    basename As String
    extension As String
    filename As String
End Type

''
' Returns information about a file path
' @param    String  path          The path being checked.
' @return   STRUCTURE_PATH_INFO   A structure with the elements: dirname, basename, extension (if any), and filename.
' @author   Stefan Thoolen <mail@stefanthoolen.nl>
Public Function pathinfo(filename As String) As STRUCTURE_PATH_INFO
    Dim ret As STRUCTURE_PATH_INFO, p() As String
    
    ' Splits the basename and the dirname
    p = Split(filename, "\")
    ret.basename = p(UBound(p))
    ret.dirname = Left(filename, Len(filename) - Len(ret.basename))
    
    ' Splits the extension and filename
    p = Split(ret.basename, ".")
    If UBound(p) > LBound(p) Then
        ret.extension = p(UBound(p))
        ret.filename = Left(ret.basename, Len(ret.basename) - Len(ret.extension))
    Else
        ret.extension = ""
        ret.filename = ret.basename
    End If
    
    ' Removes additional stuff
    If Right(ret.dirname, 1) = "\" Then ret.dirname = Left(ret.dirname, Len(ret.dirname) - 1)
    If Left(ret.extension, 1) = "." Then ret.extension = Right(ret.extension, Len(ret.extension) - 1)
    
    ' Returns the return value
    pathinfo = ret
End Function