javascript – Listing all files in my application


I want to backup my application and upload it to my bucket on S3. But for that, I first need to list the directories (along with the files), and then upload to S3.

Ifound this tutorial but it doesn't suit me, because you have to put each path to list the files, which makes the code unnecessarily larger than it should be.

NOTE: My application is made in Node, along with Express and Angular.

Could someone give me a light? 😀


Here is a function we use on the new MooTools website.

var path = require('path');
var fs = require('fs');
function getFiles(dir, files_, fileType){

    var regex = fileType ? new RegExp('\\' + fileType + '$') : '';

    return fs.readdirSync(dir).reduce(function(allFiles, file){
        var name = path.join(dir, file);
        if (fs.statSync(name).isDirectory()){
            getFiles(name, allFiles, fileType);
        } else if (file.match(regex)){
        return allFiles;
    }, files_ || []);


The function is synchronous and accepts 3 arguments :

  • the board
  • an array of files already in memory (this argument is also used by the function when calling itself)
  • the file type/extension

It returns an array of all files within the directory and sub-directories.

Scroll to Top