var fs = require("fs"),
util = require("util"),
resArr = [];
path = "D:/A-Project/Map-Web/static/images/hd_res"; //在这里修改你要搜索的路径
function explorer(path) {
fs.readdir(path, function (err, files) {
//err 为错误 , files 文件名列表包含文件夹与文件
if (err) {
console.log('error:' + err);
return;
}
files.forEach(function (file) {
fs.stat(path + '/' + file, function (err, stat) {
if (err) {
console.log(err);
return;
}
if (stat.isDirectory()) {
// 如果是文件夹遍历
explorer(path + '/' + file);
} else {
// 读出所有的文件
console.log('文件名:' + path + '/' + file);
resArr.push(file);
}
});
});
});
}
explorer(path);
setTimeout(() => {
console.log(resArr);
fs.writeFile("data.json", JSON.stringify(resArr), (err) => {
if (err)
console.log(err);
else {
console.log("File written successfully");
}
});
}, 3000);
//视文件夹多少而定时间
获取文件夹下所有目录
于 2022-04-25 15:02:39 首次发布