6
$\begingroup$

I have an external program saving files to some directory on my hard disc. I'd like to run a script that imports and runs a script on each file as it is added to the directory. Is there a simple way to do this using Dynamic? Is it necessary to use Refresh?

For example, I'd like to be able to save a series of text files to a directory (with arbitrary names) and then print the contents in near real-time.

$\endgroup$
2
  • $\begingroup$ You may find this question related. Also, maybe you can run Mathematica script (which do what you want with that files), with that external program so Mathematica do not have to constantly monitor your directory? If you can, then you can use: automatically run initialization cell $\endgroup$
    – Kuba
    Commented Sep 6, 2013 at 7:43
  • $\begingroup$ @Kuba Unfortunately I can't really modify the outside application placing files in the directory. Also, I read through the question you referenced - coupled with a very high refresh rate, it would be very helpful to have a simple function that returns the chronologically newest file in the directory (i.e. the file with the modified date closest to AbsoluteTime) but surely there must be a simpler way to proceed than the test code linked in the question? $\endgroup$
    – R.Astare
    Commented Sep 6, 2013 at 8:12

1 Answer 1

6
$\begingroup$

How about using ScheduledTask? This sets up a Monitor button that if pushed, starts monitoring the given directory ($UserBaseDirectory). If you set timeRes to a smaller value, you can get closer to realtime updating. By specifying nb beforehand, one can redirect printing to the actual notebook. The monitor checks if there is any newly added file in the directory, and lists them immediately in the notebook (unless files were removed, as then an empty list is returned). It is easy to put any further script at the indicated part of the code to e.g. check the contents of the new file(s).

files = FileNames["*", "C:\\Users\\Z\\AppData\\Roaming\\Mathematica"];
active = False;
nb = EvaluationNotebook[];
timeRes = .2;

{Button["Monitor", task = RunScheduledTask[
    active = True;
    old = files;
    files = FileNames["*", $UserBaseDirectory];
    If[old =!= files,
     newFiles = Complement[files, old];
     NotebookWrite[nb, {
       Cell["New files are:", "Output"],
       Cell[BoxData@newFiles, "Output"]}
      ];
     (* Put your code here. This will be evaluated if the contents of 
        the directory change. *)
     ],
    timeRes], Enabled -> Dynamic@Not@active],
 Button["Stop", RemoveScheduledTask@task; active=False, Enabled->Dynamic@active]}

Dynamic[ScheduledTasks[], UpdateInterval -> timeRes]
Dynamic[Column@files, UpdateInterval -> timeRes]
$\endgroup$
5
  • $\begingroup$ Thanks for your answer! However, I'm a little confused about the output... what is NotebookWrite doing? How could I, for example, grab the string corresponding to the file name last added to the directory? $\endgroup$
    – R.Astare
    Commented Sep 6, 2013 at 8:32
  • $\begingroup$ Also, I'd like to run and output some analysis scripts on each file as its imported... can I do that with this scheme? $\endgroup$
    – R.Astare
    Commented Sep 6, 2013 at 8:44
  • $\begingroup$ How can you print something in the notebook itself (not as a message) using a command inside RunScheduledTask? $\endgroup$
    – R.Astare
    Commented Sep 6, 2013 at 8:51
  • $\begingroup$ @R.Astare Please check edit, I corrected some mistakes. $\endgroup$ Commented Sep 6, 2013 at 8:55
  • $\begingroup$ Thanks - RunScheduledTask seems to be the right technique for the job at hand. Also, I can rig each file so that it appears at the end of the list of file names using something like: FileNames["","C:\\Users\\TestDir"][[Length[FileNames["", "C:\\Users\\TestDir"]]]] $\endgroup$
    – R.Astare
    Commented Sep 6, 2013 at 9:01

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.