1

I have the following folder-file structure :

Main Folder->Subfolder(500 in number)->Each subfolder having 2 to 4 images(jpeg,jpg,png,pdf)

I want to access all the images via node js all at once. (To the point-> generating a text file which gives me the URL of each image.)

How do I start about this?

xMayank
  • 1,875
  • 2
  • 5
  • 19
LGEYH
  • 63
  • 7

2 Answers2

1

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

File hierarchy:

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

Your Code

var glob = require("glob") 

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

Which will result in array

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

You can make changes accordingly.

xMayank
  • 1,875
  • 2
  • 5
  • 19
1

This is a duplicate question and you can find lot of answers here:

The easiest, shortest and most clean one maybe :

node.js fs.readdir recursive directory search

var fs = require('fs')
var path = process.cwd()
var files = []

    var getFiles = function(path, files){
        fs.readdirSync(path).forEach(function(file){
            var subpath = path + '/' + file;
            if(fs.lstatSync(subpath).isDirectory()){
                getFiles(subpath, files);
            } else {
                files.push(path + '/' + file);
            }
        });     
    }
Charlie
  • 22,886
  • 11
  • 59
  • 90