var fs = require('fs');
var events = require('events');
var util = require('util');
function Watcher(watcherDir, processedDir) {
this.watcherDir = watcherDir;
this.processedDir = processedDir;
}
Watcher.prototype = new events.EventEmitter();
Watcher.prototype.watch = function() {
var watcher = this;
fs.readdir(this.watcherDir, function(err, files) {
if (err) throw err;
files.forEach(function(value) {
watcher.emit('process', value);
});
});
};
Watcher.prototype.start = function() {
var watcher = this;
fs.watchFile(this.watcherDir, function() {
watcher.watch();
});
};
var watcher = new Watcher('./watcher', './processed');
watcher.on('process', function(file) {
var watchFile = this.watcherDir + '/' + file;
var processedFile = this.processedDir + '/' + file.toLowerCase();
fs.rename(watchFile, processedFile, function(err) {
if (err) throw err;
});
});
watcher.start();
nodejs监控目录
最新推荐文章于 2022-02-12 23:20:29 发布