Function Repository Resource:

ResetDirectoryStack

Source Notebook

Reset the value of Directory[] and DirectoryStack[]

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["ResetDirectoryStack"][]

sets the current Directory[] to $InitialDirectory and DirectoryStack[] to an empty list.

ResourceFunction["ResetDirectoryStack"][{"dir1","dir2",}]

sets DirectoryStack[] to {"dir1","dir2",}.

ResourceFunction["ResetDirectoryStack"]["cwd",{"dir1","dir2",}]

sets the current Directory[] to "cwd" in addition to setting the directory stack.

Details and Options

ResourceFunction["ResetDirectoryStack"] returns a list of the form {"cwd",{"dir1","dir2",}} where cwd is the new value of Directory[] and {"dir1","dir2",} is the value of DirectoryStack[].
In ResourceFunction["ResetDirectoryStack"]["cwd",{}], the value of "cwd" must be $InitialDirectory.
In ResourceFunction["ResetDirectoryStack"]["cwd",{,"initial"}], the value of "initial" must be $InitialDirectory.
ResourceFunction["ResetDirectoryStack"][,CreateIntermediateDirectoriesTrue] will create missing directories as needed.

Examples

Basic Examples (3) 

SetDirectory pushes a directory onto the DirectoryStack[]:

In[1]:=
SetDirectory[$InstallationDirectory];
SetDirectory[$TemporaryDirectory];
DirectoryStack[]
Out[3]=

Reset the DirectoryStack[]:

In[4]:=
ResourceFunction["ResetDirectoryStack"][]
Out[4]=

The current Directory[] and the DirectoryStack[] have returned to their original values:

In[5]:=
Directory[]
Out[5]=
In[6]:=
DirectoryStack[]
Out[6]=

Set the DirectoryStack[] to a specific list:

In[7]:=
ResourceFunction[
 "ResetDirectoryStack"][{$TemporaryDirectory, $InstallationDirectory, $InitialDirectory}]
Out[7]=

Traverse the list using ResetDirectory:

In[8]:=
ResetDirectory[]
Out[8]=
In[9]:=
ResetDirectory[]
Out[9]=
In[10]:=
ResetDirectory[]
Out[10]=

Set the current directory along with the directory stack:

In[11]:=
ResourceFunction[
 "ResetDirectoryStack"][$HomeDirectory, {$UserBaseDirectory, $InitialDirectory}]
Out[11]=
In[12]:=
Directory[]
Out[12]=
In[13]:=
DirectoryStack[]
Out[13]=

Clear the directory stack:

In[14]:=
ResourceFunction["ResetDirectoryStack"][]
Out[14]=

Scope (2) 

To reset the directory stack while preserving the current directory, use Automatic:

In[15]:=
SetDirectory[$UserBaseDirectory];
SetDirectory[$HomeDirectory]
Out[12]=
In[16]:=
ResourceFunction["ResetDirectoryStack"][Automatic]
Out[16]=

Use All to reset the current directory as well:

In[17]:=
ResourceFunction["ResetDirectoryStack"][All]
Out[17]=

ResetDirectoryStack[] is equivalent to ResetDirectoryStack[All]:

In[18]:=
SetDirectory[$UserBaseDirectory];
SetDirectory[$HomeDirectory]
Out[12]=
In[19]:=
ResourceFunction["ResetDirectoryStack"][]
Out[19]=

Options (2) 

CreateIntermediateDirectories (2) 

By default, ResetDirectoryStack will fail if any of the given directories do not exist:

In[20]:=
dir1 = FileNameJoin[{$TemporaryDirectory, CreateUUID[]}];
dir2 = FileNameJoin[{$TemporaryDirectory, CreateUUID[]}];
In[21]:=
ResourceFunction[
 "ResetDirectoryStack"][dir1, {dir2, $InitialDirectory}]
Out[21]=

Use CreateIntermediateDirectoriesTrue to create any missing directories automatically:

In[22]:=
ResourceFunction[
 "ResetDirectoryStack"][dir1, {dir2, $InitialDirectory}, CreateIntermediateDirectories -> True]
Out[22]=
In[23]:=
ResourceFunction["ResetDirectoryStack"][]
Out[23]=

Properties and Relations (1) 

Use the resource function DirectoryBlock to automatically reset the directory stack after an evaluation:

In[24]:=
ResourceFunction["DirectoryBlock"][SetDirectory[$UserBaseDirectory]; Directory[]]
Out[24]=
In[25]:=
Directory[]
Out[25]=
In[26]:=
DirectoryStack[]
Out[26]=

Possible Issues (2) 

When setting the directory stack, the final directory must be $InitialDirectory:

In[27]:=
ResourceFunction[
 "ResetDirectoryStack"][$HomeDirectory, {$UserBaseDirectory, $InitialDirectory}]
Out[27]=

If missing, it will be inserted automatically and produce a warning message:

In[28]:=
ResourceFunction[
 "ResetDirectoryStack"][$HomeDirectory, {$UserBaseDirectory}]
Out[28]=
In[29]:=
ResourceFunction["ResetDirectoryStack"][]
Out[29]=

If setting the directory stack to an empty list, the current directory must be set to $InitialDirectory:

In[30]:=
ResourceFunction["ResetDirectoryStack"][$InitialDirectory, {}]
Out[30]=

Attempting to set to another directory will result in a warning and $InitialDirectory automatically added:

In[31]:=
ResourceFunction["ResetDirectoryStack"][$HomeDirectory, {}]
Out[31]=
In[32]:=
ResourceFunction["ResetDirectoryStack"][]
Out[32]=

Version History

  • 1.0.0 – 24 February 2020

Related Resources

License Information