node.js fs.readdir recursive directory search

前端 未结 30 1534
醉酒成梦
醉酒成梦 2020-11-22 15:55

Any ideas on an async directory search using fs.readdir? I realise that we could introduce recursion and call the read directory function with the next directory to read, bu

相关标签:
30条回答
  • 2020-11-22 16:30

    I modified Trevor Senior's Promise based answer to work with Bluebird

    var fs = require('fs'),
        path = require('path'),
        Promise = require('bluebird');
    
    var readdirAsync = Promise.promisify(fs.readdir);
    var statAsync = Promise.promisify(fs.stat);
    function walkFiles (directory) {
        var results = [];
        return readdirAsync(directory).map(function(file) {
            file = path.join(directory, file);
            return statAsync(file).then(function(stat) {
                if (stat.isFile()) {
                    return results.push(file);
                }
                return walkFiles(file).then(function(filesInDir) {
                    results = results.concat(filesInDir);
                });
            });
        }).then(function() {
            return results;
        });
    }
    
    //use
    walkDir(__dirname).then(function(files) {
        console.log(files);
    }).catch(function(e) {
        console.error(e); {
    });
    
    0 讨论(0)
  • 2020-11-22 16:31

    Just in case anyone finds it useful, I also put together a synchronous version.

    var walk = function(dir) {
        var results = [];
        var list = fs.readdirSync(dir);
        list.forEach(function(file) {
            file = dir + '/' + file;
            var stat = fs.statSync(file);
            if (stat && stat.isDirectory()) { 
                /* Recurse into a subdirectory */
                results = results.concat(walk(file));
            } else { 
                /* Is a file */
                results.push(file);
            }
        });
        return results;
    }
    

    Tip: To use less resources when filtering. Filter within this function itself. E.g. Replace results.push(file); with below code. Adjust as required:

        file_type = file.split(".").pop();
        file_name = file.split(/(\\|\/)/g).pop();
        if (file_type == "json") results.push(file);
    
    0 讨论(0)
  • 2020-11-22 16:31

    A library called Filehound is another option. It will recursively search a given directory (working directory by default). It supports various filters, callbacks, promises and sync searches.

    For example, search the current working directory for all files (using callbacks):

    const Filehound = require('filehound');
    
    Filehound.create()
    .find((err, files) => {
        if (err) {
            return console.error(`error: ${err}`);
        }
        console.log(files); // array of files
    });
    

    Or promises and specifying a specific directory:

    const Filehound = require('filehound');
    
    Filehound.create()
    .paths("/tmp")
    .find()
    .each(console.log);
    

    Consult the docs for further use cases and examples of usage: https://github.com/nspragg/filehound

    Disclaimer: I'm the author.

    0 讨论(0)
  • 2020-11-22 16:32

    This one uses the maximum amount of new, buzzwordy features available in node 8, including Promises, util/promisify, destructuring, async-await, map+reduce and more, making your co-workers scratch their heads as they try to figure out what is going on.

    Node 8+

    No external dependencies.

    const { promisify } = require('util');
    const { resolve } = require('path');
    const fs = require('fs');
    const readdir = promisify(fs.readdir);
    const stat = promisify(fs.stat);
    
    async function getFiles(dir) {
      const subdirs = await readdir(dir);
      const files = await Promise.all(subdirs.map(async (subdir) => {
        const res = resolve(dir, subdir);
        return (await stat(res)).isDirectory() ? getFiles(res) : res;
      }));
      return files.reduce((a, f) => a.concat(f), []);
    }
    

    Usage

    getFiles(__dirname)
      .then(files => console.log(files))
      .catch(e => console.error(e));
    

    Node 10.10+

    Updated for node 10+ with even more whizbang:

    const { resolve } = require('path');
    const { readdir } = require('fs').promises;
    
    async function getFiles(dir) {
      const dirents = await readdir(dir, { withFileTypes: true });
      const files = await Promise.all(dirents.map((dirent) => {
        const res = resolve(dir, dirent.name);
        return dirent.isDirectory() ? getFiles(res) : res;
      }));
      return Array.prototype.concat(...files);
    }
    

    Note that starting with node 11.15.0 you can use files.flat() instead of Array.prototype.concat(...files) to flatten the files array.

    Node 11+

    If you want to blow everybody's head up completely, you can use the following version using async iterators. In addition to being really cool, it also allows consumers to pull results one-at-a-time, making it better suited for really large directories.

    const { resolve } = require('path');
    const { readdir } = require('fs').promises;
    
    async function* getFiles(dir) {
      const dirents = await readdir(dir, { withFileTypes: true });
      for (const dirent of dirents) {
        const res = resolve(dir, dirent.name);
        if (dirent.isDirectory()) {
          yield* getFiles(res);
        } else {
          yield res;
        }
      }
    }
    

    Usage has changed because the return type is now an async iterator instead of a promise

    ;(async () => {
      for await (const f of getFiles('.')) {
        console.log(f);
      }
    })()
    

    In case somebody is interested, I've written more about async iterators here: https://qwtel.com/posts/software/async-generators-in-the-wild/

    0 讨论(0)
  • 2020-11-22 16:32

    Use node-dir to produce exactly the output you like

    var dir = require('node-dir');
    
    dir.files(__dirname, function(err, files) {
      if (err) throw err;
      console.log(files);
      //we have an array of files now, so now we can iterate that array
      files.forEach(function(path) {
        action(null, path);
      })
    });
    
    0 讨论(0)
  • 2020-11-22 16:32

    Standalone promise implementation

    I am using the when.js promise library in this example.

    var fs = require('fs')
    , path = require('path')
    , when = require('when')
    , nodefn = require('when/node/function');
    
    function walk (directory, includeDir) {
        var results = [];
        return when.map(nodefn.call(fs.readdir, directory), function(file) {
            file = path.join(directory, file);
            return nodefn.call(fs.stat, file).then(function(stat) {
                if (stat.isFile()) { return results.push(file); }
                if (includeDir) { results.push(file + path.sep); }
                return walk(file, includeDir).then(function(filesInDir) {
                    results = results.concat(filesInDir);
                });
            });
        }).then(function() {
            return results;
        });
    };
    
    walk(__dirname).then(function(files) {
        console.log(files);
    }).otherwise(function(error) {
        console.error(error.stack || error);
    });
    

    I've included an optional parameter includeDir which will include directories in the file listing if set to true.

    0 讨论(0)
提交回复
热议问题