标签:sed bsp one 文件 需要 继承 case 时间 class
创建一个Watcher类来处理放在某个目录下的文件。然后可以用这个类创建一个工具,该工具可以监视目录(watch目录,将放到里面的文件名都改成小写),并将文件复制到一个单独目录(done目录)中。
扩展事件发射器需要三步:
代码:
function Watcher(watchDir, processedDir) { this.watchDir = watchDir; this.processedDir = processedDir; } var events = require(‘events‘) , util = require(‘util‘); util.inherits(Watcher, events.EventEmitter); var fs = require(‘fs‘) , watchDir = ‘./watch‘ , processedDir = ‘./done‘; Watcher.prototype.watch = function () { var watcher = this; fs.readdir(this.watchDir, function (err, files) { if (err) throw err; for (var index in files) { watcher.emit(‘process‘, files[index]); } }) }; Watcher.prototype.start = function () { var watcher = this; fs.watchFile(watchDir, function () { watcher.watch(); }); }; var watcher = new Watcher(watchDir, processedDir); watcher.on(‘process‘, function process(file) { var watchFile = this.watchDir + ‘/‘ + file; var processedDir = this.processedDir + ‘/‘ + file.toLowerCase(); fs.rename(watchFile, processedDir, function (err) { if (err) throw err; }); }); watcher.start();
标签:sed bsp one 文件 需要 继承 case 时间 class
原文地址:http://www.cnblogs.com/sumuzhe/p/7467133.html