Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Create the parent directories necessary to fill out a given file path
ResourceFunction["EnsureFilePath"][path] creates the parent directory of path if it does not already exist and returns the full path.  | 
| "string" | a file path given as a String | 
| File[…] | a File | 
| LocalObject[…] | a LocalObject | 
| CloudObject[…] | a CloudObject | 
Make sure the parent directory of a file exists:
| In[1]:= | 
| Out[1]= | 
| In[2]:= | 
| Out[2]= | 
Ensure a file path for a CloudObject:
| In[3]:= | 
| Out[3]= | 
| In[4]:= | 
| Out[4]= | 
Ensure a file path for a LocalObject:
| In[5]:= | 
| Out[5]= | 
| In[6]:= | 
| Out[6]= | 
EnsureFilePath can also handle File expressions:
| In[7]:= | 
| Out[7]= | 
EnsureFilePath does not create the target file, only the parent directory:
| In[8]:= | 
| Out[8]= | 
| In[9]:= | 
| Out[9]= | 
| In[10]:= | 
| Out[10]= | 
If the parent directory is a file that already exists, it will be moved into a directory of that name:
| In[11]:= | 
| Out[12]= | 
| In[13]:= | 
| Out[13]= | 
| In[14]:= | 
| Out[14]= | 
The same is true for a CloudObject:
| In[15]:= | 
| Out[16]= | 
| In[17]:= | 
| Out[17]= | 
| In[18]:= | 
| Out[18]= | 
Make sure a target directory exists when writing to a file:
| In[19]:= | 
| Out[19]= | 
| In[20]:= | 
| In[21]:= | 
| Out[21]= | 
The file will only be moved into the target directory if it differs by one level:
| In[22]:= | 
| Out[23]= | 
| In[24]:= | 
| Out[24]= | 
This work is licensed under a Creative Commons Attribution 4.0 International License