Function Repository Resource:

BackupFile

Source Notebook

Quickly duplicate a file with an incremented name

Contributed by: Michael Sollami

ResourceFunction["BackupFile"][]

saves a copy the current notebook with an incremented filename.

ResourceFunction["BackupFile"][nb]

saves a copy of notebook object nb with an incremented filename.

ResourceFunction["BackupFile"][file]

saves a copy of file with an incremented filename.

Details

ResourceFunction["BackupFile"] adds a 0 indexed integer counter "-n" to basenames
ResourceFunction["BackupFile"] uses CopyFile and the duplicates are placed in the same directory as the source file

Examples

Basic Examples (3) 

Backup the current notebook:

In[1]:=
ResourceFunction["BackupFile"][]
Out[1]=

The filename counter will automatically increment:

In[2]:=
ResourceFunction["BackupFile"][]
Out[2]=

Create multiple copies of a file:

In[3]:=
Table[ResourceFunction["BackupFile"]@
  FileNameJoin@{$HomeDirectory, "Downloads", "GitStatus.nb"}, 3]
Out[3]=

Backup relative files:

In[4]:=
SetDirectory[CreateDirectory[]]
Out[4]=
In[5]:=
Put[1, "myfile"]
file = File["myfile"];
FileExistsQ[file]
Out[7]=
In[8]:=
ResourceFunction["BackupFile"][file]
Out[8]=

Scope (2) 

Duplicate works for valid string paths or File objects:

In[9]:=
ResourceFunction["BackupFile"]@
 File[FileNameJoin@{$HomeDirectory, "Downloads", "GitStatus.nb"}]
Out[9]=

BackupFile works on any open or closed NotebookObject:

In[10]:=
NotebookSave[Notebook[{Cell["Hello", "Text"]}], fn = FileNameJoin[{$TemporaryDirectory, "Hello.nb"}]]
nb = NotebookOpen[fn];
In[11]:=
NotebookFileName[nb]
Out[11]=
In[12]:=
ResourceFunction["BackupFile"]@nb
Out[12]=
In[13]:=
NotebookClose[nb]; DeleteFile[
 FileNameJoin[{$TemporaryDirectory, "Hello.nb"}]]

Possible Issues (1) 

BackupFile only works on saved notebooks:

In[14]:=
nb = NotebookPut@Notebook[{Cell["Hello", "Text"]}]
Out[14]=
In[15]:=
ResourceFunction["BackupFile"]@nb
Out[16]=

Publisher

Michael Sollami

Version History

  • 1.0.0 – 18 January 2021

Related Resources

License Information