4
$\begingroup$

last evening Mathematica crashed and hours of work were gone, because I forgot to save my notebook. During homework today I just pressed Ctrl+S every few minutes, to make sure that I don't loose my progress again. However, after a few more hours I discovered that my first approach to the problem has been much better. Sadly I've overwritten my notebook every time with Ctrl+S, so my calculations from the morning were also gone.

So I decided to look for a auto-save function for mathematica. At a coding internship I once used some software, to track down all changes and create a nice version history, which led me swap between different versions of my code easily. Unfortunately I was not able to find something like this for Mathematica (if you know an existing method, please tell me!). Thus I thought I could try to write a code to autosave my progress every 10 minutes into a new file.

Basically the code can be used inside every .nb file. It creates a new directory in the notebook-directory with the name "filename_versions.. Every time you execute the code it looks up the current time and exports the notebook into the versions directory with the name filename_yyyy_mm_dd_hh_mm.nb. However, only a new version is created if the last backup-file is older than x minutes (I used 10 minutes for x).

I tried out the code a few times (I simply copied the code to the top of my homework mathematica notebook). Since I often use Ctrl+A Shift+Enter to evaluate all cells at once I also execute the autosave every time. The result was pretty nice, I had a nice history of my progress in the versions-directory and was able to look up old stuff I deleted in my code.

I want to point out, that I am a beginner at Mathematica, so if you have any suggestions how I could improve my code please tell me (It's not the most beautiful code, but it works.). I already thought of putting the code into a package, but first I want to try it out more often.

I'm looking forward to hear your opinion on this problem and my approach to solve it.

Best regards, Tobias

That's the code. Unluckily I had to delete all my nice comments to post the code nicely here.

ClearAll["Global`*"]; 
SetDirectory[NotebookDirectory[]]; 
asfilename = StringDrop[StringDrop[ToString[EvaluationNotebook[]], 17], -6]; 
asdirectoryname = StringJoin[asfilename, "_versions"];
Quiet[If[DirectoryQ[asdirectoryname], False, CreateDirectory[asdirectoryname]]];
asdate = Take[DateList[], 5]; 
asdate = Table[If[asdate[[i]] > 9, ToString[asdate[[i]]], StringJoin["0", ToString[asdate[[i]]]]], {i, Length[asdate]}]; 
asbackupfilename = StringJoin[asfilename, "_", ToString[asdate[[1]]], "_", ToString[asdate[[2]]], "_", ToString[asdate[[3]]], "_", ToString[asdate[[4]]], "_", ToString[asdate[[5]]]];
astimeinterval = Quantity[10, "Minutes"]; 
asfilesindir = FileNames[StringJoin[asfilename, "*.nb"], asdirectoryname]; 
asfiledates = StringDrop[(StringTake[#1, -19] & )[asfilesindir], -3];
asfiledates = Table[{StringTake[asfiledates[[i]], 4], StringTake[StringTake[asfiledates[[i]], 7], -2], StringTake[StringTake[asfiledates[[i]], 10], -2], StringTake[StringTake[asfiledates[[i]], 13], -2], 
 StringTake[StringTake[asfiledates[[i]], 16], -2]}, {i, Length[asfiledates]}]; 
asfiledates = ToExpression /@ asfiledates;
asfiledates = Table[DateObject[asfiledates[[i]]], {i, Length[asfiledates]}];
asdatecompare = Table[If[DateDifference[asfiledates[[i]], Date[], "Minute"] < astimeinterval, 1, 0], {i, Length[asfiledates]}]; 
asexp := Export[FileNameJoin[{NotebookDirectory[], asdirectoryname, StringJoin[asbackupfilename, ".nb"]}], EvaluationNotebook[]];
If[Total[asdatecompare] < 1, asexp]; 
$\endgroup$
  • $\begingroup$ Hi, welcome to Mathematica.stackexchange. I don't want to discourage you, it is a very important issue but as currently stated it rather fits more discussion friendly community.wolfram.com. We could still try to review your code, but could you consider posting it instead of attaching a notebook? Alternatively, maybe there are topics where your code would fit as an answer, see: 13997, 11258, 6435 $\endgroup$ – Kuba Dec 11 '17 at 22:13
  • $\begingroup$ Hi, I've already posted the same on community.wolfram.com, but no one answered, so I decided to post it here. If it's better to directly post the code here, I'll do that and also answer to the questions you mentioned. $\endgroup$ – Tobias Mitterdorfer Dec 11 '17 at 22:27
  • 1
    $\begingroup$ I see. It is quicker to take a look at a code than to fetch a notebook. p.s. Answer only if your input qualifies as an answer. Also, please read asking guidelines. $\endgroup$ – Kuba Dec 11 '17 at 22:32
  • 1
    $\begingroup$ Have you seen NotebookAutoSave? $\endgroup$ – Edmund Dec 12 '17 at 0:22

Your Answer

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

Browse other questions tagged or ask your own question.