Function Repository Resource:

RelativePath

Source Notebook

Create a file path relative to a given directory

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["RelativePath"][path]

gives a string corresponding to path relative to the current Directory[].

ResourceFunction["RelativePath"][path1,path2]

gives a string corresponding to path2 relative to path1.

Details and Options

ResourceFunction["RelativePath"][,OperatingSystem"os"] treats file paths as formatted for the specified operating system.
Possible values for OperatingSystem are "Windows", "MacOSX" and "Unix", and it uses the value of $OperatingSystem by default.

Examples

Basic Examples (2) 

Get a file path specification relative to another:

In[1]:=
ResourceFunction["RelativePath"]["path/to/file", "path/another/file"]
Out[1]=

Get a path name relative to the current directory:

In[2]:=
ResourceFunction["RelativePath"][$InstallationDirectory]
Out[2]=
In[3]:=
ResourceFunction["RelativePath"][$TemporaryDirectory]
Out[3]=
In[4]:=
ResourceFunction["RelativePath"][$UserBaseDirectory]
Out[4]=

Scope (5) 

Path names can be given as a file:// URI:

In[5]:=
ResourceFunction[
 "RelativePath"]["file:///path/to/dir", "file:///path/file"]
Out[5]=

Use a mix of file specifications:

In[6]:=
path1 = $UserBaseDirectory
Out[6]=
In[7]:=
path2 = $LocalBase
Out[7]=
In[8]:=
ResourceFunction["RelativePath"][path1, path2]
Out[8]=

File objects are also supported:

In[9]:=
ResourceFunction["RelativePath"][File["path/dir"], File["path/to/file"]]
Out[9]=

Get a relative path using cloud objects:

In[10]:=
ResourceFunction["RelativePath"][CloudObject["path/to/this/file"], CloudObject["path/to/that/file"]]
Out[10]=

If a cloud object is given with CloudObjectNameFormat set as "UUID", RelativePath can still work if the file exists:

In[11]:=
obj = CloudPut[1, "path/to/this/file", CloudObjectNameFormat -> "UUID"]
Out[11]=
In[12]:=
ResourceFunction["RelativePath"][obj, CloudObject["path/to/that/file"]]
Out[12]=

Options (1) 

OperatingSystem (1) 

Specify a different operating system for path name formatting:

In[13]:=
ResourceFunction[
 "RelativePath"]["C:\\Users\\Birdnardo", "C:\\Windows", OperatingSystem -> "Windows"]
Out[13]=
In[14]:=
ResourceFunction["RelativePath"]["C:\\Windows", "D:\\Stuff", OperatingSystem -> "Windows"]
Out[14]=

Properties and Relations (2) 

The single-argument form gives paths relative to the current directory:

In[15]:=
SetDirectory[$UserDocumentsDirectory]
Out[15]=
In[16]:=
relative = ResourceFunction["RelativePath"][$UserBaseDirectory]
Out[16]=

Get the absolute path using AbsoluteFileName:

In[17]:=
AbsoluteFileName[relative]
Out[17]=

Get the absolute path using ExpandFileName:

In[18]:=
ExpandFileName[relative]
Out[18]=

Return to the original directory:

In[19]:=
ResetDirectory[]
Out[19]=

RelativePath[NotebookDirectory[],path] is a partial inverse for the resource function NotebookRelativePath:

In[20]:=
path = ResourceFunction["NotebookRelativePath"]["path/to/file"]
Out[20]=
In[21]:=
ResourceFunction["RelativePath"][NotebookDirectory[], path]
Out[21]=

Possible Issues (1) 

Using Windows paths with OperatingSystem"Unix" will produce strange results:

In[22]:=
ResourceFunction["RelativePath"]["C:\\Windows", "D:\\Stuff", OperatingSystem -> "Unix"]
Out[22]=

Version History

  • 1.0.0 – 18 February 2020

Related Resources

License Information