How can I check if a filesystem path is absolute or relative in a cross-platform way?
For my purposes, I consider a path absolute if points to a location that is independent of Directory[]
.
I only need to check paths valid on the current operating system. I never need to check Windows paths on Unix or vice versa.
Examples of absolute paths on Unix:
/foo/bar
~/baz
(This is sometimes considered relative as it is user$UserName
-dependent. However, it is notDirectory[]
-dependent.)
On Windows:
F:\foo\bar
F:/foo/bar
Relative paths on Unix:
foo/bar
.
,./foo
,..
Relative paths on Windows:
foo\bar
.
,.\bar
,..
, etc.\foo\bar
(May be either ofC:\
orD:\
, etc.)- any of the above with a
/
separator instead of\
These are just a few examples. I may have missed several special cases. I am looking for something which always works and preferably doesn't depend on my own partial understanding of what forms an absolute or relative path may take on different OSs. I would rather not start matching strings, at least not without a reference document which gives an exhaustive list of all possible path syntaxes both for absolute and relative paths.
The ideal solution would use a feature that asks the operating system directly: