Listing all the files in my application

Asked

Viewed 878 times

3

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

I found 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 done in Node, along with Express and Angular.

Could someone give me a light? : D

  • Do you want to list files including subfolders? Which operating system?

  • I managed to do @Caffé, listed them and now I’m trying to store the code of these files in a variable. You would know how to do this?

  • Possibly yes. Update your question or delete and create a new one if the need has completely changed.

  • @Marceloalves posts there his solution so that other people can see when they have the same doubt you had. Then create a new question with this new demand you had so staff can help you better.

1 answer

1

Here’s a feature we use on the new Mootools site.

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)){
            allFiles.push(name);
        }
        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 subdirectories.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.