best way to get folder and file list in Javascript best way to get folder and file list in Javascript javascript javascript

best way to get folder and file list in Javascript


In my project I use this function for getting huge amount of files. It's pretty fast (put require("fs") out to make it even faster):

var _getAllFilesFromFolder = function(dir) {    var filesystem = require("fs");    var results = [];    filesystem.readdirSync(dir).forEach(function(file) {        file = dir+'/'+file;        var stat = filesystem.statSync(file);        if (stat && stat.isDirectory()) {            results = results.concat(_getAllFilesFromFolder(file))        } else results.push(file);    });    return results;};

usage is clear:

_getAllFilesFromFolder(__dirname + "folder");


fs/promises and fs.Dirent

Here's an efficient, non-blocking ls program using Node's fast fs.Dirent objects and fs/promises module. This approach allows you to skip wasteful fs.exist or fs.stat calls on every path -

// main.jsimport { readdir } from "fs/promises"import { join } from "path"async function* ls (path = "."){ yield path  for (const dirent of await readdir(path, { withFileTypes: true }))    if (dirent.isDirectory())      yield* ls(join(path, dirent.name))    else      yield join(path, dirent.name)}async function* empty () {}async function toArray (iter = empty()){ let r = []  for await (const x of iter)    r.push(x)  return r}toArray(ls(".")).then(console.log, console.error)

Let's get some sample files so we can see ls working -

$ yarn add immutable     # (just some example package)$ node main.js
[  '.',  'main.js',  'node_modules',  'node_modules/.yarn-integrity',  'node_modules/immutable',  'node_modules/immutable/LICENSE',  'node_modules/immutable/README.md',  'node_modules/immutable/contrib',  'node_modules/immutable/contrib/cursor',  'node_modules/immutable/contrib/cursor/README.md',  'node_modules/immutable/contrib/cursor/__tests__',  'node_modules/immutable/contrib/cursor/__tests__/Cursor.ts.skip',  'node_modules/immutable/contrib/cursor/index.d.ts',  'node_modules/immutable/contrib/cursor/index.js',  'node_modules/immutable/dist',  'node_modules/immutable/dist/immutable-nonambient.d.ts',  'node_modules/immutable/dist/immutable.d.ts',  'node_modules/immutable/dist/immutable.es.js',  'node_modules/immutable/dist/immutable.js',  'node_modules/immutable/dist/immutable.js.flow',  'node_modules/immutable/dist/immutable.min.js',  'node_modules/immutable/package.json',  'package.json',  'yarn.lock']

For added explanation and other ways to leverage async generators, see this Q&A.


Why to invent the wheel?

There is a very popular NPM package, that let you do things like that easy.

var recursive = require("recursive-readdir");recursive("some/path", function (err, files) {  // `files` is an array of file paths  console.log(files);});

Lear more: