Sub Main
    
    Dim str As String   
    
    str = GetDocFullPath()       ' "/home/user/document.ods"                   '
    MsgBox(GetFileBaseName(str)) ' "document.ods"                              '
    
    str = "C:\Windows\system32\drivers\etc\hosts"
    MsgBox(GetFileBaseName(str)) ' "hosts"                                     '
    
    str = "test.ods"
    MsgBox(GetFileBaseName(str)) ' "test.ods"                                  '
    
    str = "many.dots.in.file.name.ods"
    MsgBox(GetFileBaseName(str)) ' "many.dots.in.file.name.ods"                '
    
End Sub

' Returns the filename of the given path.                                      '
' For example "/home/user/document.ods" input will return "document.ods".      '
' ByVal keyword prevents FullPath from modification because by default         '
' arguments are passed by reference.                                           '
Function GetFileBaseName(ByVal FullPath As String) As String

    Dim i As Long
    Dim PathLen As Long
    Dim PathURL As String
    ' Converting to URL for Linux/Windows compatibility.                       '
    '   URL notation does not allow certain special characters to be used.     '
    '   These are either replaced by other characters or encoded. A slash      ' 
    '   (/) is used as a path separator. For example, a file referred to as    ' 
    '   C:\My File.sxw on the local host in "Windows notation" becomes         '
    '   file:///C|/My%20File.sxw in URL notation.                              '
    ' https://help.libreoffice.org/Basic/Basic_Glossary                        '
    PathURL = ConvertToURL(FullPath)
    ' FullPath could be mistakenly converted to http. For example:             '
    ' ConvertToURL("many.dots.in.file.name.ods") will be misinterpreted.       ' 
    If Left(PathURL,7) <> "file://" Then 
        PathURL = ConvertToURL("/" + FullPath)
    End If
    PathLen = Len(PathURL)
    For i = PathLen To 1 Step -1
        If Mid(PathURL,i,1) = "/" Then
            GetFileBaseName = ConvertFromURL(Right(PathURL,PathLen - i))
            Exit For
        End If
    Next i

End Function

' Returns document full path if document has a path. Returns empty string if   '
' document has no path and IgnoreNoPathError flag is set to TRUE.              '
Function GetDocFullPath(Optional IgnoreNoPathError as Boolean) As String
    
    ' Default behavior is to return empty string if document location is empty.'
    ' This can happen if document is new and not saved thus do not have a path.'
    If NOT ThisComponent.hasLocation() AND IgnoreNoPathError <> TRUE Then
        ' Err.Raise is not valid statement but will generate error anyway.     '
        Err.Raise("Document has no path. Probably because is not saved.")
    End If
    
    GetDocFullPath = ConvertFromURL(ThisComponent.getLocation())
    
End Function

Описание:
Вышепредставленный программный код на бейсике для макросов либреофиса можно использовать чтобы получить имя файла и его расширение из полного пути к файлу.


P.S. Присылайте криптовалюту если вам нравится то, что я делаю.
Если не нравится - тоже присылайте.
LTC (Litecoin): LLN6X2uV1iuQ1e4tdmQZsf2RRwh4pxPSej

Leave a Reply

*