Sub Main
Dim str As String
str = GetDocFullPath() ' "/home/user/document.ods" '
MsgBox(GetBaseDirectory(str)) ' "/home/user/" '
MsgBox(GetBaseDirectory(str,TRUE)) ' "/home/user" '
str = "C:\Windows\system32\drivers\etc\hosts"
MsgBox(GetBaseDirectory(str)) ' "C:\Windows\system32\drivers\etc\" '
MsgBox(GetBaseDirectory(str,TRUE)) ' "C:\Windows\system32\drivers\etc" '
str = "test.ods"
MsgBox(GetBaseDirectory(str)) ' "" as there is no base directory '
MsgBox(GetBaseDirectory(str,TRUE)) ' "" as there is no base directory '
str = "many.dots.in.file.name.ods"
MsgBox(GetBaseDirectory(str)) ' "" as there is no base directory '
MsgBox(GetBaseDirectory(str,TRUE)) ' "" as there is no base directory '
End Sub
' Returns the base directory of the given path. '
' For example "/home/user/document.ods" input will return "/home/user/". '
' If DropTrailingSlash is TRUE, the example above will return "/home/user". '
' ByVal keyword prevents FullPath from modification because by default '
' arguments are passed by reference. '
Function GetBaseDirectory(ByVal FullPath As String, Optional DropTrailingSlash As Boolean) As String
Dim i As Long
Dim PathLen As Long
Dim PathURL As String
Dim LenDiff As Long
Dim BaseName 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("name.with.many.dots.myext") 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
BaseName = ConvertFromURL(Right(PathURL,PathLen - i))
Exit For
End If
Next i
If DropTrailingSlash = TRUE Then
BaseName = "/" + BaseName
End If
LenDiff = Len(FullPath) - Len(BaseName)
If LenDiff < 0 Then
LenDiff = 0
End If
GetBaseDirectory = Left(FullPath,LenDiff)
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
Описание:
Вышепредставленный программный код на бейсике для макросов либреофиса можно использовать чтобы из полного пути к файлу получить директорию в которой находится файл.