I built a macro that is to be used in a Linux and a Windows environment which needs access to several files relative to the location of the current file.
I got the macro working correctly in Linux, then remembering this statement from Andrew D. Pitonyak
So before migrating to windows I changed all explicit "/" to GetPathSeparator() and a colleague tested it on window.URL notation is system independent, so URL paths work as well on an Apple computer as they do on a Windows computer. To create a system-specific path, use the function GetPathSeparator to obtain the system-specific path separator. Listing 3 demonstrates how to use GetPathSeparator to build a complete path. Windows-based computers use “\” as the path separator, and Unix-based computers use “/” as the path separator. URL notation uses “/” as the separator regardless of the operating system.
This was the original code
Code: Select all
oDoc = ThisComponent
If (oDoc.hasLocation()) Then
sDocURL = oDoc.getURL()
sDirectoryName = DirectoryNameoutofPath(sDocURL, "/")
End If
url = ConvertToURL(sDirectoryName) & "/" & "ReplacementCharacters.ods"
If Not FileExists(url) Then
MsgBox( url & " doesn't exist - see instruction manual")
exit sub
End If
Code: Select all
oDoc = ThisComponent
If (oDoc.hasLocation()) Then
sDocURL = oDoc.getURL()
sDirectoryName = DirectoryNameoutofPath(sDocURL, GetPathSeparator())
End If
url = ConvertToURL(sDirectoryName) & GetPathSeparator() & "ReplacementCharacters.ods"
If Not FileExists(url) Then
MsgBox( url & " doesn't exist - see instruction manual")
exit sub
End If
On Linux the macro found the correct filefile:///C:/Users/alex/Documents/website/extracts/test.odt\ReplacementCharacters.ods doesn't exist - see instruction manual
So I reverted back to using "/" instead of GetPathSeparator() and the macro worked in Linux as well as Windows, which confirms Andrew's statementfile:///home/alex/Documents/website/extracts/ReplacementCharacters.ods
above.URL notation uses “/” as the separator regardless of the operating system.
Both Windows and Linux then both correctly looked for and found the file
My question is under what circumstances would one use GetPathSeparator()?