Donald A Subert Donald A Subert - 1 year ago 107
Javascript Question

Node.js watch a directory for incoming archive by scp

I have a task to create a node.js script that listens in a directory for new archive files to arrive and process them.

I see that I can do this with

The files are
archives, that arrive via

The problem is that the arrival of a new archive file seems to create multiple (the exact number is unpredictable) events in the file-system. The first is a
, followed by some number of

I need to reliably trigger my processing logic only once, when the archive is finished being transferred. How can this be done?

Additional notes:

  • I am not able to make changes to the system sending the archive, only
    the system receiving it.

  • I am not considering using elapsed time to
    guess that the scp event has concluded. That is not reliable.

Answer Source

Using the watch library you can do:

var watch = require('watch')
watch.createMonitor('/home/path', function (monitor) {
   monitor.on("created", function (file, stat) {
       // do work with new file

I recommend taking advantage of the rich library ecosystem available to you because you're using the node platform. Some of these problems have already been solved for you!