# Updating the name of nb file after closing/running it

Suppose I have a nb file named as 240119.nb [date of today]. Now If tomorrow I open it and do something, then upon closing it, It should be renamed automatically as 250119.nb. Any suggestion please ?

• This saves a copy of the current Notebook: NotebookSave[EvaluationNotebook[], NotebookDirectory[] <> "new file name.nb"] – banone Jan 24 at 12:20

Try the following command:

ClearAll[eventAction];
eventAction[]:=Module[{
$$previousPath,$$todaysPath
},
$$previousPath=NotebookFileName[EvaluationNotebook[]];$$todaysPath=FileNameJoin[{
NotebookDirectory[],
"NameChangingNotebook - "<>DateString[Today,{"Day","Month","YearShort"}]<>".nb"
}];
NotebookSave[EvaluationNotebook[],$$todaysPath]; DeleteFile[$$previousPath];
];

SetOptions[EvaluationNotebook[],NotebookEventActions->{"WindowClose":>(eventAction[])}]


I am not sure about what the current notebook save event is called. It doesn't seem to be documented maybe someone here can help!

If the EventHandler doesn't work in the meantime when you are done with your work in the notebook, you can just execute the eventAction[] yourself.

Based on the suggestion by @Michael E2, I have added the NotebookEventActions. Try it and let me know @math if it works. Make sure to have a backup copy of your notebook in another folder!

Unfortunately, I don't have access to the desktop notebook frontend right now so I can't test it myself.

• Would RenameFile be safer? – Michael E2 Jan 25 at 14:34
• @MichaelE2 I don't think you can change the current notebook's name while it is still open (i.e. from the same notebook). A renamed copy is necessary in this case. – user13892 Jan 25 at 14:41
• @MichaelE2 do you know what the current notebook save event is called? – user13892 Jan 25 at 14:46
• "WindowClose", I think. See also NotebookEventActions. – Michael E2 Jan 25 at 14:58