Get short file name : File Name « File Path « VBA / Excel / Access / Word






Get short file name

 
Sub Main()
   Debug.Print GetShortName("c:a\a\a.xls")
End Sub
Function GetShortName(sLongName As String) As String
    Dim sPath As String
    Dim sShortName As String

    BreakdownName sLongName, sShortName, sPath

    GetShortName = sShortName
End Function
Sub BreakdownName(sFullName As String, _
                  ByRef sname As String, _
                  ByRef sPath As String)

    Dim nPos As Integer
    nPos = FileNamePosition(sFullName)
    If nPos > 0 Then
        sname = Right(sFullName, Len(sFullName) - nPos)
        sPath = Left(sFullName, nPos - 1)
    Else
        'Invalid sFullName - don't change anything
    End If
End Sub
Function FileNamePosition(sFullName As String) As Integer
    Dim bFound As Boolean
    Dim nPosition As Integer

    bFound = False
    nPosition = Len(sFullName)

    Do While bFound = False
        If nPosition = 0 Then Exit Do
        If Mid(sFullName, nPosition, 1) = "\" Then
            bFound = True
        Else
            ' Working right to left
            nPosition = nPosition - 1
        End If
    Loop

    If bFound = False Then
        FileNamePosition = 0
    Else
        FileNamePosition = nPosition
    End If
End Function

 








Related examples in the same category

1.Get file name