PhantomJS学习笔记(3):遍历文件系统
28 Nov 2012再来一个官方demo,遍历系统文件。
命令:phantomjs scandir.js /
从根目录开始递归遍历。
代码:
// List all the files in a Tree of Directories
var system = require('system');
if (system.args.length !== 2) {
console.log("Usage: phantomjs scandir.js DIRECTORY_TO_SCAN");
phantom.exit(1);
}
var scanDirectory = function (path) {
var fs = require('fs');
if (fs.exists(path) && fs.isFile(path)) {
console.log(path);
} else if (fs.isDirectory(path)) {
fs.list(path).forEach(function (e) {
if ( e !== "." && e !== ".." ) { //< Avoid loops
scanDirectory(path + '/' + e);
}
});
}
};
scanDirectory(system.args[1]);
phantom.exit();

转载请注明:于哲的博客 » PhantomJS学习笔记(3):遍历文件系统