代码如下:
const path = require("path");
const fs = require("fs");
const pathName = "./";//要读取的文件夹路径,windows系统路径"\"要转义,如:"C:\\Users\\user\\Desktop"
fs.readdir(pathName, function(err, files){
let fileArr = [];
let dirArr = [];
let otherArr = [];
(function iterator(i){
if(i == files.length) {//读取完成输出
console.log({"files":fileArr,"dirs":dirArr,"others":otherArr});
return ;
}
fs.stat(path.join(pathName, files[i]), function(err, data){
if(data.isFile()){//判断是否为文件
fileArr.push(path.extname(files[i]));
}else if(data.isDirectory()){//判断是否为文件夹/目录
dirArr.push(files[i]);
}else{//其他类型,如快捷方式
otherArr.push(files[i]);
}
iterator(i+1);
});
})(0);
});