跳至主要內容

nodejs递归读取所有文件

Mr.Chen技术Nodejs小于 1 分钟约 85 字

nodejs 递归读取所有文件

var fs = require('fs')
var path = require('path')

function readFileList(dir, filesList = []) {
  const files = fs.readdirSync(dir)
  console.log(files)
  files.forEach((item, index) => {
    var fullPath = path.join(dir, item)
    const stat = fs.statSync(fullPath)
    if (stat.isDirectory()) {
      readFileList(path.join(dir, item), filesList) //递归读取文件
    } else {
      filesList.push(fullPath)
    }
  })
  return filesList
}

var filesList = []
readFileList(__dirname, filesList)
上次编辑于: