const fs = require("fs")const path = require("path")const readDir = (entry)=>{const dirInfo = fs.readdirSync(entry) //["hello","node.js"]dirInfo.forEach(item=>{const location = path.join(entry,item)//需要判断这个路径是文件夹还是文件const info = fs.statSync(location)if(info.isDirectory()){ //如果是文件夹的话,就进来了 helloconsole.log(`dir:${location}`)readDir(location) //递归的方式实现遍历}else{console.log(`file:${location}`)}})}readDir(__dirname)
