如何在nodejs中使用fs从多个目录获取文件名?

我想从所有的目录中获取文件信息一旦我有,我有进一步的代码实现,但我卡在这里,并得到错误粘贴的问题任何想法是什么在下面的代码中实现错误?

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(); 

错误

 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 

readdir是asynchronous的,所以你需要等待它的callback来执行进一步的操作。

很难知道究竟是什么问题,所以我包含了日志语句。

编辑

 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... });