参考网站:http://zhuanlan.zhihu.com/FrontendMagazine/19750470
nodejs demo:
var fs = require('fs');
var path = require('path');
function readDir (dir,cb){
fs.readdir(dir, function(err,files){
if(err) return cb(err);
var count = files.length;
var stats = [];
files.forEach(function(file,index){
fs.stat(path.join(dir,file), function(err,stat){
stat['path'] = path.join(dir,file);
stats[index] = stat;
if (--count == 0) {
var largest = stats
.filter(function (stat) { return stat.isFile() })
.reduce(function (prev, next) {
if (prev.size > next.size) return prev
return next
});
console.log(files[stats.indexOf(largest)]);
}
});
});
});
}
readDir('D:\\Program Files\\nodejs',function(err,response){
//TODO
});