Skip to content

遍历指定目录所有文件

ts
import * as fs from 'fs';  
import * as path from 'path';

export function getFiles(dir:string) {
  const files = fs.readdirSync(dir);
  let fileList:any = [];

  files.forEach((file:any) => {
    const filePath = path.join(dir, file)
    if (fs.statSync(filePath).isDirectory()) {
      // 目录,继续遍历
      fileList = fileList.concat(getFiles(filePath))
    } else {
      fileList.push(filePath)
    }
  })

  return fileList
}