nodejs获取指定路径下所有的文件夹名或类型
let components = []
const files = fs.readdirSync('./components')
files.forEach(function (item, index) {
let stat = fs.statSync("./components/" + item)
if (stat.isDirectory() === true) {
components.push(item)
}
})
console.log(components);
//=============================
//检测文件夹下文件是文件夹还是文件 模块
const fs = require('fs');
function textFile(str) {
let arr = fs.readdirSync(str);
console.log(arr);
let obj = {};
arr.forEach((val, idx) => {
// console.log(val)
let ph = fs.statSync(str + '/' + val);
if (ph.isDirectory()) {
// console.log(`${val} 是文件夹`);
obj[val] = '文件夹';
} else {
// console.log(`${val} 是文件`);
obj[val] = '文件';
}
})
return obj;
}
const my = require('./my');
console.log(my.textFile('./homework'));
本文来自博客园,作者:JackieDYH,转载请注明原文链接:https://www.cnblogs.com/JackieDYH/p/17634705.html