Skip to content

Instantly share code, notes, and snippets.

@amay077
Created October 2, 2013 10:15
Show Gist options
  • Select an option

  • Save amay077/6791613 to your computer and use it in GitHub Desktop.

Select an option

Save amay077/6791613 to your computer and use it in GitHub Desktop.
ディレクトリを再帰的にたどってファイル一覧を出力する ref: http://qiita.com/amay077/items/cc6ee3e66040a5097230
var fs = require("fs")
, path = require("path")
, dir = process.argv[2] || '.'; //引数が無いときはカレントディレクトリを対象とする
var walk = function(p, fileCallback, errCallback) {
fs.readdir(p, function(err, files) {
if (err) {
errCallback(err);
return;
}
files.forEach(function(f) {
var fp = path.join(p, f); // to full-path
if(fs.statSync(fp).isDirectory()) {
walk(fp, fileCallback); // ディレクトリなら再帰
} else {
fileCallback(fp); // ファイルならコールバックで通知
}
});
});
};
// 使う方
walk(dir, function(path) {
console.log(path); // ファイル1つ受信
}, function(err) {
console.log("Receive err:" + err); // エラー受信
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment