Function Repository Resource:

MonitorFolder

Source Notebook

Asynchronously monitor the contents of a folder for the appearance of new files

Contributed by: Jon McLoone

ResourceFunction["MonitorFolder"][path,fn]

asynchronously monitor the folder path and apply fn to any new files that appear.

Details and Options

ResourceFunction["MonitorFolder"] starts a ScheduledTask and returns the corresponding TaskObject.
Two options are provided:
UpdateInterval1time between checks of the folder
"FilePattern""*"kinds of files to watch for

Examples

Basic Examples (2) 

Print out the file names of any new files that appear in the Pictures folder:

In[1]:=
task = ResourceFunction["MonitorFolder"]["Users/user/Pictures", Print]
Out[1]=

To stop monitoring the folder use TaskRemove:

In[2]:=
TaskRemove[task]
Out[2]=

Options (2) 

By default the folder is checked every second. To check less frequently use the option UpdateInterval:

In[3]:=
ResourceFunction["MonitorFolder"][$InstallationDirectory, Print, UpdateInterval -> 60]
Out[3]=

All new files are detected, but a "FilePattern" option allows only specific files to be watched for. For example only watch for files ending in ".txt":

In[4]:=
ResourceFunction["MonitorFolder"][$InstallationDirectory, Print, "FilePattern" -> "*.txt"]
Out[4]=

Applications (1) 

If any new images appear in the Pictures folder, display their contents in a new window:

In[5]:=
ResourceFunction["MonitorFolder"]["Users/user/Pictures", CreateDocument[Import[#], WindowSize -> All] &, "FilePattern" -> {"*.jpg", "*.jpeg", "*.png"}]
Out[5]=

Properties and Relations (1) 

The monitoring is performed by a ScheduledTask and so will be performed even if the kernel is busy with another operation:

In[6]:=
task = ResourceFunction["MonitorFolder"]["Users/user/Pictures", Print];
Pause[10];

Publisher

Jon McLoone

Version History

  • 1.0.0 – 15 October 2019

License Information