-
Notifications
You must be signed in to change notification settings - Fork 2
/
basic_extras_GetBaseDirectory.bas
80 lines (76 loc) · 5.75 KB
/
basic_extras_GetBaseDirectory.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'--------------------------------------------------------------------------------------------------'
' GetBaseDirectory '
'--------------------------------------------------------------------------------------------------'
' Returns parsed base directory of a given path. '
' '
' Parameters: '
' '
' ByVal FullPath As String '
' Path to parse. ByVal keyword prevents it from modification because by default arguments are '
' passed by reference. '
' '
' Optional DropTrailingSlash As Boolean <Default = FALSE> '
' Drops trailing slash at the end of parsed base directory. '
' '
' Examples: '
'--------------------------------------------------------------------------------------------------'
' '
' basedir = GetBaseDirectory(path) '
' '
' Expected values: '
' path: "/home/user/document.ods" basedir: "/home/user/" '
' path: "/home/user/" basedir: "/home/user/" '
' path: "/home/user" basedir: "/home/" '
' path: "C:\Users\Admin\Рабочий стол" basedir: "C:\Users\Admin\" '
' path: "C:\Users\Admin\" basedir: "C:\Users\Admin\" '
' path: "C:\Users\Admin" basedir: "C:\Users\" '
'--------------------------------------------------------------------------------------------------'
' '
' basedir = GetBaseDirectory(path, TRUE) '
' '
' Expected values: '
' path: "/home/user/document.ods" basedir: "/home/user" '
' path: "/home/user/" basedir: "/home/user" '
' path: "/home/user" basedir: "/home" '
' path: "C:\Users\Admin\Рабочий стол" basedir: "C:\Users\Admin" '
' path: "C:\Users\Admin\" basedir: "C:\Users\Admin" '
' path: "C:\Users\Admin" basedir: "C:\Users" '
'--------------------------------------------------------------------------------------------------'
' Feedback & Issues: '
' https://github.com/aa6/libreoffice_calc_basic_extras/issues '
'--------------------------------------------------------------------------------------------------'
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
' Fetching file base name from FullPath. '
' 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
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