3
$\begingroup$

I am trying to create a Dynamic code that will Import the contents of file only if it has been modified. I tried the following code:

DynamicModule[{
    fileDate=FileDate[file,"Modification"]
},
    fileContents=Import[file];"-- Last Updated: "<>DateString[]<> " --",
    TrackedSymbols:>{fileDate}
]

Needless to say, the file is big and takes a lot of time to import thus it should be dynamically checked whether it has been updated and if it has then the newest version of the file is imported right away.

$\endgroup$

1 Answer 1

2
$\begingroup$

The following checks the file last modification date every 10 seconds. If it is different from the current file date, the file is imported and a message is printed. TaskRemove[obj] will remove the task.

fileDate = FileDate[file];
fileContents = Import[file];
obj = SessionSubmit[
   ScheduledTask[FileDate[file], Quantity[10, "Seconds"]], 
   HandlerFunctions -> <|
     "ResultReceived" -> (If[#[["EvaluationResult"]] =!= fileDate, 
         fileDate = #[["EvaluationResult"]]; 
         Print["Importing on " ~~ DateString[Now]]; 
         fileContents = Import[file]] &)|>, 
   HandlerFunctionsKeys -> {"EvaluationResult"}];
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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