码迷,mamicode.com
首页 > 其他好文 > 详细

node递归属性目录结构

时间:2016-02-18 22:48:45      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

要求,读取结束后才能输出所有文件

var fs = require(‘fs‘);
var path = require(‘path‘);

var list = [];
var count = 0;
function readDir(_path, callback) {

var toExec = function (_path) {
count++;
fs.readdir(_path, function (err, files) {
if (err) {
console.log(err);
return;
}
files.forEach(function (file, i) {
var stat = fs.lstatSync(path.join(_path, file));
if (stat.isFile()) {
list.push(path.join(_path, file));
} else if (stat.isDirectory()) {
toExec(path.join(_path, file));
}

if ((i + 1) === files.length) {
count--;
}

if(count === 0){
callback(list);
}
});
});
};

toExec(_path);
};

readDir(path.join(process.cwd(), ‘go‘), function (_list) {
console.log(_list);
});

node递归属性目录结构

标签:

原文地址:http://www.cnblogs.com/ajun/p/5199328.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!