Help us understand the problem. What is going on with this article?

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

More than 5 years have passed since last update.

@_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); // エラー受信
});
amay077
ランチの時は呼ぶといい!
https://blog.amay0777.net/
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
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  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
ユーザーは見つかりませんでした