Title: How to get files from multiple directories using fs or async methods?
Tags: javascript;node.js;fs;async.js
Question: I am trying to write logic where i can get files information from each directory , How can i achieve that using ```async``` or any ```fs``` method. I am passing ```fileInfo``` and ```filePath``` to function ```compareDates``` for further implementation. With below code i dont see any results.

controller.js

```var cron = require('cron-job');
var fs = require('fs');
var path = require('path');
var async = require('async');
var directories = ['./logs/dit', './logs/st','./logs/uat']
function cronJob() {
directories.forEach(function(dir){
    var files = fs.readdirSync(dir);
    async.eachSeries(files, function(file, callback) {
            var filePath = path.join(dirPath, file);
            var fileInfo = {};
            fs.stat(filePath, function(err, stats) {
               if (err) {
                 console.info("File doesn't");
                } else {
                 fileInfo.fileDate = stats.birthtime;
                 fileInfo.filename = file;
                 compareDates(fileInfo,filePath);
                 console.log(fileInfo);

               }
            });
        });
})

}

cronJob();
```

Error

```throw err;  // Forgot a callback but don't know where? Use NODE_DEBUG=fs
```

Comment: i see that error in console when it invoke `var files = fs.readdirSync(dir);`

Comment: no I am not , any better way to get this done

Comment: What's the actual error message? And where did you find that line of code?

Comment: That's weird. Are you sure you're not using `fs.readdir`?


Here is another answer: This function will return an array of file paths for all files inside a directory. It recursively loops through all folders and subfolders within the specified dir and ignores empty folders.

```var walk = function(dir, done) {
    var results = [];
    fs.readdir(dir, function(err, list) {
        if (err) return done(err);
        var pending = list.length;
        if (!pending) return done(null, results);
        list.forEach(function(file) {
            file = path.resolve(dir, file);
            fs.stat(file, function(err, stat) {
                if (stat &amp;&amp; stat.isDirectory()) {
                    walk(file, function(err, res) {
                        results = results.concat(res);
                        if (!--pending) done(null, results);
                    });
                } else {
                    results.push(file);
                    if (!--pending) done(null, results);
                }
            });
        });
    });
};
```

Usage:

```walk(directory, function(err, results) {
    // throw error if something is wrong
    if (err) throw err;

    results.forEach(function(path) {
        // do stuff with the path
    });

});
```

Note that the function will return paths relative to ```/``` or ```C:\```; if you want your directories to be relative to the location of your server/app.js file, you'll need to slice off the length of ```__dirname``` from the beginning of each path.

You can then use ```fs.stat``` on each of the files to get the information you need to pass to your function.

Using your code, it would be like:

```var directories = ['/logs/dit', '/logs/st','/logs/uat'];
directories.forEach(function(directory){
    walk(directory, function(listOfFiles) {
        listOfFiles.forEach(function(file) {
            fs.stat(file, function(err, stats) {
                if (err) throw err;
                    var fileDate = stats.birthtime;
                    var filename = file;
                    compareDates(filename, filePath);
            });
        });
    });
});
```

Apologies in advance if my code is a bit messy. Some things you could do :


use a ```for``` loop instead of ```forEach``` if you need to keep
count of files.
repurpose ```walk``` and utilize the ```fs.stat``` call thats being made in there.


Hope this helps :)

Comment for this answer: i am getting error `at Array.forEach (native)
 at cronJob (C:\Users`
Comment for this answer: `
ReferenceError: walk is not defined
    at C:\Users\sh529u\WebstormProjects\Ulog-0\ulog\app\serverfiles\cronJobs.js:
10:6
    at Array.forEach (native)
    at C:\Users\sh529u\WebstormProjects\Ulog-0\ulog\app\serverfiles\cronJobs.js:
9:14
    at Array.forEach (native)
    at cronJob (C:\Users\sh529u\WebstormProjects\Ulog-0\ulog\app\serverfiles\cro
nJobs.js:7:13)
    at Object. (C:\Users\sh529u\WebstormProjects\Ulog-0\ulog\app\serv
erfiles\cronJobs.js:42:1)`
Comment for this answer: i fixed that `filename` period
Comment for this answer: ok i added that and see `TypeError: listOfFiles.forEach is not a function
    at C:\Users\sh529u\WebstormProjects\Ulog-0\ulog\app\serverfiles\cronJobs.
34:21
    at C:\Users\sh529u\WebstormProjects\Ulog-0\ulog\app\serverfiles\cronJobs.`
Comment for this answer: could you paste the error stack? You might have to normalize the paths before doing any computations on them if you are running on an OS other than linux (i.e. windows)
There was also a period in `var filename = file` (assuming you copied and pasted the code)
Comment for this answer: You have to define the walk function. Copy the walk function and paste it ABOVE your `cron` function. 
Also, you should use backticks ```` for the stacks. helps with readability :)
Comment for this answer: That's really odd, since `listOfFiles` is an array of strings... You can try using a for loop to iterate through the items instead.
`for (i = 0; i < listOfFiles.length; i++){
  //code goes here
}`. If you can, use a console log and JSON.stringify listOfFiles to see its output. that should help you understand what you need to do to loop through those items or figure out where the problem is.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:03/15/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<javascript><node.js><fs><async.js>', 'Title': 'How to get files from multiple directories using fs or async methods?', 'ViewCount': 760, 'id': 42815426, 'user_AboutMe': None, 'user_AccountId': 4174040.0, 'user_DisplayName': 'hussain', 'user_Reputation': 6045.0, 'user_Views': 662.0, 'user_id': 6014098.0}}