2
$\begingroup$

This code will create a time-stamped backup of the current Notebook. How can I execute it every 5 minutes?

task = { sec = ToString@NumberForm[AbsoluteTime[], 10, ScientificNotationThreshold -> {-10, 10}]; originalFile = NotebookFileName[]; name = FileNameTake[originalFile, -1]; fileName = StringSplit[name, "."][[1]]; fname = StringJoin[{fileName, "-", sec, "nb"}]; pathName = FileNameJoin[{$TemporaryDirectory, fname}]; Export[File[pathName], EvaluationNotebook[]]; }; 

I've been trying to use SessionSubmit, ScheduledTask & TaskResume but keep getting a message:

TaskResume::taskid: A string or a TaskObject is expected instead of $Failed. 
$\endgroup$

1 Answer 1

3
$\begingroup$
  • I guess, in the new task session, you can not use the expected NotebookName[] existing in the caller session.

  • You need to run the below code in a named notebook file.

(*Capture the notebook's file name before the scheduled task*) originalFile = NotebookFileName[]; (*Define the backup function with originalFile as a parameter*) backupNotebook[originalFile_] := Module[{sec, name, fileName, fname, pathName}, sec = ToString@ NumberForm[AbsoluteTime[], 10, ScientificNotationThreshold -> {-10, 10}]; Print["originalFile=", originalFile]; name = FileNameTake[originalFile, -1]; Print["name=", name]; fileName = StringSplit[name, "."][[1]]; fname = StringJoin[{fileName, "-", sec, ".nb"}]; pathName = FileNameJoin[{$TemporaryDirectory, fname}]; Print["pathName=", pathName]; Export[File[pathName], EvaluationNotebook[]];]; (*Create and start the scheduled task, passing originalFile*) {period, count} = {3, 4}; task = SessionSubmit[ ScheduledTask[backupNotebook[originalFile], {period, count}]] (*Optional:Control the task*) TaskExecute[task]; (*TaskResume[task];*) (*TaskAbort[task];*) (*TaskRemove[task];*) 
$\endgroup$
1
  • $\begingroup$ Thanks, that completes the functionality I immediately need. $\endgroup$ Commented Sep 17, 2024 at 2:46

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.