Function Repository Resource:

FileNameWithDate

Source Notebook

Add a date stamp to a file name or replace an existing one

Contributed by: Robert B. Nachbar

ResourceFunction["FileNameWithDate"][filename]

returns a new filename string with a date stamp inserted before the file extension.

Details

ResourceFunction["FileNameWithDate"] will replace any existing date stamp.

Examples

Basic Examples (1) 

Add a date stamp to a file name:

In[1]:=
ResourceFunction["FileNameWithDate"]["function_code.m"]
Out[1]=

Scope (5) 

The input can be a File object:

In[2]:=
ResourceFunction["FileNameWithDate"][File["output.log"]]
Out[2]=

Replace a date stamp with a new one:

In[3]:=
ResourceFunction[
 "FileNameWithDate"]["function_code--2021-03-18-13-34-49.m"]
Out[3]=

The input can contain directory information:

In[4]:=
ResourceFunction["FileNameWithDate"]["~/Documents/report.nb"]
Out[4]=

The filename does not need to have a file type:

In[5]:=
ResourceFunction["FileNameWithDate"]["bashrc"]
Out[5]=

The input filename can represent a directory:

In[6]:=
ResourceFunction["FileNameWithDate"][
 FileNameJoin[{"~", "Documents", "test"}]]
Out[6]=

Applications (1) 

Create a copy of a file with a date-stamped filename before editing the original file:

In[7]:=
With[{file = FileNameJoin[{$UserBaseDirectory, "Kernel", "init.m"}]},
 CopyFile[file, ResourceFunction["FileNameWithDate"][file]]
 ]
Out[7]=

Possible Issues (1) 

The input must not be an empty string:

In[8]:=
ResourceFunction["FileNameWithDate"][""]
Out[8]=

Publisher

Robert Nachbar

Version History

  • 1.0.0 – 24 March 2021

Related Resources

License Information