Created
October 2, 2013 10:15
-
-
Save amay077/6791613 to your computer and use it in GitHub Desktop.
ディレクトリを再帰的にたどってファイル一覧を出力する ref: http://qiita.com/amay077/items/cc6ee3e66040a5097230
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
| 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