//引入模块
const fs=require("fs");
const path=require("path");
/**
* @param filePath 读取文件/目录的路径
* @param tabNum 文件前面的空格
*/
const showDir=(filePath,tabNum=0)=>{
//读取目录
fs.readdir(filePath,(err,files)=>{
if(err){
console.log('读取失败');
return;
}
files.forEach((ele)=>{
console.log('\t'.repeat(tabNum),ele);
//获取子目录的路径
const childPath=path.resolve(filePath,ele);
//判断子目录是否仍然是目录,如果是继续深入,不是则计算大小
fs.stat(childPath,(err,stats)=>{
if(err){
console.log('判断失败');
return
}
if(stats.isDirectory()){
return showDir(childPath,tabNum+1)
}
});
})
})
}
showDir('F:\\AAA课堂案例')
最终效果图如下