How to: monitor local folders in mftJS and upload files as they appear in it

This article refers to the mftJS language, which is the special “flavor” of JavaScript designed by Syncplify for the upcoming Syncplify.me MFT! software product.

Let’s see the code first, then we’ll explain the details.

 

As you can see, what we have here is a pretty granular file-system watcher. The WatchDir method adds a folder to the watcher, and its second parameter (boolean set to true in the above example) tells the watcher to also watch all sub-folders.

We then disable the notification for renamed files (.NotifyRename = false), and set InclusionFilter and ExclusionFilter. Please note that these filters are arrays of strings, so you can include/exclude as many patterns/masks as you please.

Once started (.Start) the file-system watcher will start running. We then need to query it for all pending events with the .Events() method. Running it inside an endless loop (while true) will ensure it runs for as long as the script is in execution.

We’ve also introduced a Sleep(1000) (sleeps for 1000 milliseconds, or 1 second) function to save CPU cycles and avoid checking the queue of pending events too often.

Then we Upload (.UploadWithPath to preserve the original file’s path) every file that is written, and that’s it: we have a real-time, always-on, backup that follows our pre-determined set of rules.

Print Friendly, PDF & Email
Bookmark the permalink.

Comments are closed.