Open barretlee opened 9 years ago
var fs = require('fs');
var Path = require('path');
var ROOT = "../../../work";
var ret = [];
var output = "./output.json";
function walk(path){
var files = fs.readdirSync(path);
files.forEach(function(file){
var filePath = Path.join(path, file);
if(/node_module/.test(filePath)) return;
if(fs.statSync(filePath).isDirectory()){
walk(filePath);
} else {
if(/\.json$/.test(file)){
console.log('> DEBUG: ' + filePath);
ret.push({
name: file,
path: filePath/*,
file: fs.readFileSync(filePath)*/
});
}
}
});
}
walk(ROOT);
fs.writeFileSync(output, JSON.stringify(ret, null, 2));
同步方式写代码。
问题:遍历某个目录下所有的 json 文件,将这些文件的文件名和文件目录保存到指定的位置。
后续思考: