//引入文件模块
const fs = require('fs')
function fn(path) {
//读取路径中的文件
const files = fs.readdirSync(path)
// 遍历每个读取到的文件
for (let name of files) {
//拼接路径
const stat = fs.statSync(path + '/' + name)
//判断读取到的路径是否是文件夹
if (stat.isFile()) {
console.log(name, '文件');
} else if (stat.isDirectory()) {
console.log(name, '文件夹');
fn(path + '/' + name) // 递归
}
}
}
fn('D:/前端/demo')
读取用户指定文件下的所有文件
最新推荐文章于 2024-03-01 20:13:50 发布