如何检查目录中多个文件的统计信息并作为json发送给客户端?

时间:2022-08-24 20:37:39

Lets assume i have three files in directory i want to check stats for all these files and send birthtimes to client ,Its always returning one file stats as you can see in data. How can i get stats for all files from directory ?

让我们假设我在目录中有三个文件我想检查所有这些文件的统计数据并将生育时间发送给客户端,它总是返回一个文件统计数据,你可以在数据中看到。如何从目录中获取所有文件的统计信息?

app.js

  var path = './logs/ditLogs'
  fs.stat(path, function (err,stats) {
        console.log('STATS',stats);
        fileData.birthtime = stats.birthtime;
        //callback(stats.mtime);
    });

data

{ birthtime: Tue Jul 12 2016 09:33:14 GMT-0400 (Eastern Daylight Time),
  filename: ['server.log', 'server1.log' ] }

1 个解决方案

#1


1  

Async library is the way to go. http://caolan.github.io/async/docs.html

异步库是要走的路。 http://caolan.github.io/async/docs.html

I would suggest something like this

我会建议这样的事情

var dirPath = './logs/ditLogs'; //this will get you list of all files. in directory var files = fs.readdirSync(dirPath); var objToReturn = {}; //then using async do like this async.eachSeries(files, function (file, callback) { var filePath = path.join(dirPath, file); fs.stat(filePath, function(err, stats){ //write stats data into objToReturn callback(); }); }, function(err){ //final callback when all files completed here send objToReturn to client });

var dirPath ='。/ logs / datLogs'; //这将为您提供所有文件的列表。在目录var files = fs.readdirSync(dirPath); var objToReturn = {}; //然后使用async做这样的async.eachSeries(文件,函数(文件,回调){var filePath = path.join(dirPath,file); fs.stat(filePath,function(err,stats){//写入统计信息数据到objToReturn callback();});},function(err){//当这里完成所有文件时最终回调将objToReturn发送给客户端});

Hope this helps.

希望这可以帮助。

#1


1  

Async library is the way to go. http://caolan.github.io/async/docs.html

异步库是要走的路。 http://caolan.github.io/async/docs.html

I would suggest something like this

我会建议这样的事情

var dirPath = './logs/ditLogs'; //this will get you list of all files. in directory var files = fs.readdirSync(dirPath); var objToReturn = {}; //then using async do like this async.eachSeries(files, function (file, callback) { var filePath = path.join(dirPath, file); fs.stat(filePath, function(err, stats){ //write stats data into objToReturn callback(); }); }, function(err){ //final callback when all files completed here send objToReturn to client });

var dirPath ='。/ logs / datLogs'; //这将为您提供所有文件的列表。在目录var files = fs.readdirSync(dirPath); var objToReturn = {}; //然后使用async做这样的async.eachSeries(文件,函数(文件,回调){var filePath = path.join(dirPath,file); fs.stat(filePath,function(err,stats){//写入统计信息数据到objToReturn callback();});},function(err){//当这里完成所有文件时最终回调将objToReturn发送给客户端});

Hope this helps.

希望这可以帮助。