2
$\begingroup$

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 ?

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

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];
];

(*DynamicModule[{},EventHandler[Null,{{"MenuCommand","Save"}:>(eventAction[])}]];*)

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ Would RenameFile be safer? $\endgroup$ – Michael E2 Jan 25 at 14:34
  • $\begingroup$ @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. $\endgroup$ – user13892 Jan 25 at 14:41
  • $\begingroup$ @MichaelE2 do you know what the current notebook save event is called? $\endgroup$ – user13892 Jan 25 at 14:46
  • $\begingroup$ "WindowClose", I think. See also NotebookEventActions. $\endgroup$ – Michael E2 Jan 25 at 14:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.