Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

filewatcher nodejs

const events = require("events"),
  util = require("util");

const fs = require("fs"),
  watchDir = "./watch",
  processedDir = "./done";
  
  /*Let's extend events.EventEmitter in order to be able
  to emit and listen for event*/
  
  class Watcher extends events.EventEmitter {
  constructor(watchDir, processedDir) {
    super();
    this.watchDir = watchDir;
    this.processedDir = processedDir;
  }

/* Cycles through directory and process any file
found emitting a process event for each one*/

  watch() {
    const watcher = this;
    fs.readdir(this.watchDir, function(err, files) {
      if (err) throw err;
      for (let index in files) {
        watcher.emit("process", files[index]);
      }
    });
  }

/* Start the directory monitoring 
leveraging Node's fs.watchFile */

  start() {
    var watcher = this;
    fs.watchFile(watchDir, function() {
      watcher.watch();
    });
  }
}
 
 /* Let's instantiate our Watcher object 
 passing to the constructor our folders path */
 
let watcher = new Watcher(watchDir, processedDir);


/*Let's use the on method inherited from 
event emitter class to listen for process 
events and move files from source folder 
to destination*/

watcher.on("process", function process(file) {
  const watchFile = this.watchDir + "/" + file;
  const processedFile = this.processedDir + "/" + file.toLowerCase();
  fs.rename(watchFile, processedFile, function(err) {
    if (err) throw err;
  });
});

/*Start it!!!*/

watcher.start();
Comment

PREVIOUS NEXT
Code Example
Javascript :: app script with success handler response null 
Javascript :: useLinkPressHandler 
Javascript :: how get value of datePicker in react 
Javascript :: javascript llenar array con objetos 
Javascript :: Cannot resolve name `object`.Flow 
Javascript :: Decimal Base Exponent shorthand javascript 
Javascript :: Array.find Shorthand javascript 
Javascript :: kitten ui input height multiline 
Javascript :: different way to for loop js 
Javascript :: Add a mirgation in sequelize 
Javascript :: disable jquery ajax call on init 
Javascript :: call url many times 
Javascript :: get current user moralis web3 login 
Javascript :: translate from json string to object c# 
Javascript :: Foreach array in JavaScript fsd 
Javascript :: custu, loading next js 
Javascript :: como tanformar um paraafrafo para maiusculaavascript 
Javascript :: email validation in form using javascript 
Javascript :: javascript grow function 
Javascript :: how to display value in input field using innerText in js 
Javascript :: typeorm sqlite Using Repositories 
Javascript :: joi validation error message in path parameter value array to string 
Javascript :: material ui refresh icon 
Javascript :: outlet context remix js 
Javascript :: Register post meta of sidebar in wordpress 
Javascript :: react clearinterval outside of useefect 
Javascript :: set value 
Javascript :: salesforce js merge object 
Javascript :: how to pass jsp variable as parameter via onclick function in html 
Javascript :: jquery console.log object file 
ADD CONTENT
Topic
Content
Source link
Name
8+7 =