34
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ディレクトリを再帰的にたどってファイル一覧を出力する

Posted at

@_shimizu さんの

の派生品です。ファイルパスを出力するだけにしました。

enumFilesRecursive.js
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); // エラー受信
});
34
33
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
33

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?