Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
33
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@amay077

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

@_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); // エラー受信
});
33
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
33
Help us understand the problem. What is going on with this article?