1

I want to get file information from all the directories once i have that i have further implementation of code, but i am stuck here and getting error that is pasted in question any idea what is implemented wrong in below code ?

cron.js

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.readdir(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;
                 console.log(fileInfo);
//                 compareDates(fileInfo,filePath);
//                 callback();
               }
            });
        });
})

}

cronJob();

Error

s\Ulog-0\ulog\app\serverfiles\logs\dit'
  at Error (native)
  at Object.fs.readdirSync (fs.js:808:18)
  at C:\Users\WebstormProjects\Ulog-0\ulog\app\serverfiles\cronJob
20
  at Array.forEach (native)
  at cronJob (C:\Users\WebstormProjects\Ulog-0\ulog\app\serverfile
obs.js:7:13)
  at Object.<anonymous> (C:\Users\\WebstormProjects\Ulog-0\ulog\app
hussain
  • 6,587
  • 18
  • 79
  • 152
  • You should probably read the directories asynchronously, so that there are no unexpected behaviours. – Mikey Mar 16 '17 at 14:31

1 Answers1

2

readdir is asynchronous so you need to wait for its callback to execute further operations.

It's hard to know what exactly is the problem, so I have included log statements.

Edit

var fs = require('fs');
var path = require('path');
var async = require('async');
var directories = ['/../../logs/dit', '/../../logs/st', '/../../logs/uat'];

// loop through each directory
async.eachSeries(directories, function (dir, cb1) {
    var dir = __dirname + dir;
    console.log('reading', dir);
    // get files for the directory
    fs.readdir(dir, function (err, files) {
        if (err) return cb1(err);

        // loop through each file
        async.eachSeries(files, function (file, cb2) {
            var filePath = path.resolve(dir + '/' + file);
            // get info for the file
            fs.stat(filePath, function (err, stats) {
                if (err) return cb2(err);
                var fileInfo = { fileDate: stats.birthtime, filename: file };
                console.log('fileInfo', fileInfo);
                compareDates(fileInfo, filePath);
                cb2(null, fileInfo);
            });
        }, cb1);

    });
}, function (err, fileInfos) {
    if (err) {
        console.info('error', err);
        return;     
    }
    // when you're done reading all the files, do something...
});
Mikey
  • 6,728
  • 4
  • 22
  • 45
  • getting this error `C:\Users\sh529u\WebstormProjects\Ulog-0\ulog\app\serverfiles>node cronJobs.j reading ./logs/dit dir error { [Error: ENOENT: no such file or directory, scandir 'C:\Users\sh5 WebstormProjects\Ulog-0\ulog\app\serverfiles\logs\dit'] errno: -4058, code: 'ENOENT', syscall: 'scandir', path: 'C:\\Users\\sh529u\\WebstormProjects\\Ulog-0\\ulog\\app\\serverfiles` – hussain Mar 16 '17 at 15:11
  • `logs` folder is on root level and function `app/serverfiles/cronJob.js ` – hussain Mar 16 '17 at 15:12
  • Well, you know what the problem is then. It has to do with the paths in your `directories` array. [This answer](http://stackoverflow.com/questions/8131344/what-is-the-difference-between-dirname-and-in-node-js) might help you fix that. – Mikey Mar 16 '17 at 15:19
  • this is really confusing me i am new to nodejs if you can help me resolve this issue i will appreciate it – hussain Mar 16 '17 at 15:31
  • See my edit. I changed some of the logic. Particularly, I changed the `directories` array. Ensure that the paths are relative to the current directory. If `logs` is on the root level (assuming same level as `app`), then you need to move two levels up `/../../`. Notice also the use of [__dirname](https://nodejs.org/docs/latest/api/globals.html#globals_dirname) to know where to start from. Just play around with the paths and eventually you'll get it. – Mikey Mar 16 '17 at 18:01
  • Thank you boss it worked i will definitely take a look on _dirname and path stuff. – hussain Mar 16 '17 at 18:25