Get all files recursively in directories NodejS

I have a little problem with my function. I would like to get all files in many directories. Currently, I can retrieve the files in the file passed in parameters. I would like to retrieve the html files of each folder in the folder passed as a parameter. I will explain if I put in parameter “test” I retrieve the files in “test” but I would like to retrieve “test / 1 / *. Html”, “test / 2 / . /.html “:

var srcpath2 = path.join('.', 'diapo', result);
function getDirectories(srcpath2) {
                return fs.readdirSync(srcpath2).filter(function (file) {
                    return fs.statSync(path.join(srcpath2, file)).isDirectory();
                });
            }

The result :
[1,2,3]

  • Asynchronously reading and caching multiple files in nodejs
  • Read/Writing files in node with fs via UNC path
  • Node fs copy a folder
  • NodeJS File Statistics
  • Saving an image stored on s3 using node.js?
  • How to access name of file within fs callback methods?
  • thanks !

  • What is the current directory used by fs module functions?
  • write/add data in JSON file using node.js
  • Use Chokidar for watching over files with specific extentions
  • Node.js: How to check if folder is empty or not with out uploading list of files
  • How to reset nodejs stream?
  • Find absolute base path of the project directory
  • 4 Solutions collect form web for “Get all files recursively in directories NodejS”

    It looks like the glob npm package would help you. Here is an example of how to use it:

    File hierarchy:

    test
    ├── one.html
    └── test-nested
        └── two.html
    

    JS code:

    var getDirectories = function (src, callback) {
      glob(src + '/**/*', callback);
    };
    getDirectories('test', function (err, res) {
      if (err) {
        console.log('Error', err);
      } else {
        console.log(res);
      }
    });
    

    which displays:

    [ 'test/one.html',
      'test/test-nested',
      'test/test-nested/two.html' ]
    

    I needed to so something similar, in an Electron app: get all subfolders in a given base folder, using TypeScript, and came up with this:

    import { readdirSync, statSync, existsSync } from "fs";
    import * as path from "path";
    
    // recursive synchronous "walk" through a folder structure, with the given base path
    getAllSubFolders = (baseFolder, folderList = []) => {
    
        let folders:string[] = readdirSync(baseFolder).filter(file => statSync(path.join(baseFolder, file)).isDirectory());
        folders.forEach(folder => {
            folderList.push(path.join(baseFolder,folder));
            this.getAllSubFolders(path.join(baseFolder,folder), folderList);
        });
    }
    

    You can also write your own code like below to traverse the directory as shown below :

    var fs = require('fs');
    function traverseDirectory(dirname, callback) {
      var directory = [];
      fs.readdir(dirname, function(err, list) {
        dirname = fs.realpathSync(dirname);
        if (err) {
          return callback(err);
        }
        var listlength = list.length;
        list.forEach(function(file) {
          file = dirname + '\\' + file;
          fs.stat(file, function(err, stat) {
            directory.push(file);
     if (stat && stat.isDirectory()) {
              traverseDirectory(file, function(err, parsed) {
         directory = directory.concat(parsed);
         if (!--listlength) {
           callback(null, directory);
         }
       });
     } else {
         if (!--listlength) {
           callback(null, directory);
         }
              }
          });
        });
      });
    }
    traverseDirectory(__dirname, function(err, result) {
      if (err) {
        console.log(err);
      }
      console.log(result);
    });
    

    You can check more information about it here : http://www.codingdefined.com/2014/09/how-to-navigate-through-directories-in.html

    Here’s mine. Like all good answers it’s hard to understand:

    const isDirectory = path => statSync(path).isDirectory();
    const getDirectories = path =>
        readdirSync(path).map(name => join(path, name)).filter(isDirectory);
    
    const isFile = path => statSync(path).isFile();  
    const getFiles = path =>
        readdirSync(path).map(name => join(path, name)).filter(isFile);
    
    const getFilesRecursively = (path) => {
        let dirs = getDirectories(path);
        let files = dirs
            .map(dir => getFilesRecursively(dir)) // go through each directory
            .reduce((a,b) => a.concat(b), []);    // map returns a 2d array (array of file arrays) so flatten
        return files.concat(getFiles(path));
    };
    
    Node.js is the Best Javascript runtime in the world.