首页 > 编程语言 >nodejs 遍历文件夹下所有文件

nodejs 遍历文件夹下所有文件

时间:2022-11-12 11:11:06浏览次数:43  
标签:files 遍历 nodejs currentPath fileArr 文件夹 file getDirs dir

const fs = require("fs");
let fileArr = [];
const dir = "E:/Hbuilder-Content/7.19新增页面";

main(dir);
console.log(fileArr);

function main(currentPath) {
  if (currentPath === dir && fileArr.length != 0) return fileArr;
  fileArr = [];
  getDirs(currentPath);
  function getDirs(path) {
    let files;
    try {
      files = fs.readdirSync(path, { encoding: "utf8", withFileTypes: true });
    } catch (error) {
      return;
    }
    files.forEach((file) => {
      if (file.isDirectory()) {
        getDirs(path + "/" + file.name);
      } else {
        fileArr.push(dir + "/" + file.name);
      }
    });
  }
}

 

标签:files,遍历,nodejs,currentPath,fileArr,文件夹,file,getDirs,dir
From: https://www.cnblogs.com/laremehpe/p/16882964.html

相关文章