const fs = require('fs');
let rootPath = __dirname + '\\\\' + 'a';
let obj = querydir(rootPath);
let str = JSON.stringify(obj, "", "\t");
fs.writeFile('./data.json', str, function (err) {
if (err) { console.log('文件写入错误', err) }
})
function querydir(path) {
let list = fs.readdirSync(path);
if (list && list.length > 0) {
return list.map(name => {
let dir = path + '/' + name;
let isFile = fs.statSync(dir);
if (isFile && isFile.isDirectory()) {
return { class: name, name, child: querydir(dir, name) };
} else {
return { name, path: dir.replace(rootPath, '') }
}
})
}
}
网友评论