caLLowCreation

File Watcher

Apr 18th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2. const fs = require('fs');
  3.  
  4. // Declare needed variables
  5. const filename = 'most_recent_follower.txt';
  6. const filelocation = 'F:/Web/nodejs/naivebot/server/data/streamlabels/';
  7. const filePath = filelocation + filename;
  8. const changedFiles = {};
  9.  
  10. // watch file for changes
  11. fs.watchFile(filePath, { encoding: 'utf8' }, (cur, prev) => {
  12.  
  13.     // read file content for comparison
  14.     const fileInfo = fs.readFileSync(filePath, 'utf8').trim();
  15.  
  16.     // make file change comparison
  17.     // check existing object info (changedFiles[filename])
  18.     // and compare it to the new data (fileInfo)
  19.     const fileChanged = changedFiles[filename] !== fileInfo;
  20.  
  21.     // if changes we have a new event not a repeat
  22.     if (fileChanged === true) {
  23.         // send message to chat here
  24.         console.log('Send message to chat here');
  25.     }
  26.     // assign file content to the object for future reference
  27.     changedFiles[filename] = fileInfo;
  28. });
Add Comment
Please, Sign In to add comment