Last active
March 28, 2017 07:55
-
-
Save atez/0e1c190756b20cd174626a6538f6049b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
fs.watchFile('./test.txt', function (cur, pre) { | |
if (!pre.isFile() && cur.isFile()) { | |
console.log('==>file created'); | |
} | |
else if (pre.isFile() && !cur.isFile()) { | |
console.log('==>file deleted'); | |
} | |
else if (pre.isFile() && cur.isFile() && Date.parse(pre.mtime) !== Date.parse(cur.mtime)) { | |
console.log('==>file modified'); | |
} | |
}); | |
//开始监视时test.txt必须存在 | |
const watcher = fs.watch('./test.txt', function (event, filename) { | |
// event为change或rename | |
console.log(event, filename); | |
if (event === 'rename') { | |
console.log('文件不存在,监视停止'); | |
watcher.close(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment