return 使用 node js 过滤文件的所有文件

return all the files with filtered files using node js

我想要 return 文件夹和子文件夹中的文件,但需要过滤扩展名以 .html、.htm 或 .aspx 结尾的文件

我有一个代码,return 只有扩展名为 Index.html, Default.htm, Index.aspx 的文件也需要其余文件,但不知道如何 return 文件的其余部分连同此过滤扩展

async function getAllFile(folderPath) {
  let files = await fs.readdir(folderPath);
  files = await Promise.all(
    files.map(async (file) => {
      const filePath = path.join(folderPath, file);
      const stats = await fs.stat(filePath);
      if (stats.isDirectory()) {
        return getAllFile(filePath);
      } else if (stats.isFile()) return filePath;
    })
  );
  return files.reduce((all, folderContents) => all.concat(folderContents), []);
}
const filenames = new Set([
  "index.html",
  "index.htm",
  "index.aspx",
  "default.html",
  "default.htm",
  "default.aspx",
]);

const filterFiles = async (folderPath) => {
  let filename, parts;
  const paths = await getAllFile(folderPath);
  const filteredFiles = paths.filter((filePath) => {
    parts = filePath.split("/");
    filename = parts[parts.length - 1];
    return filenames.has(filename.toLowerCase());
  });
  return filteredFiles;
};

您的 filterFiles 可以 return 具有 filteredFilesallFiles 的对象。

例如

const filterFiles = async (folderPath) => {
  let filename, parts;
  const paths = await getAllFile(folderPath);
  const filteredFiles = paths.filter((filePath) => {
    parts = filePath.split("/");
    filename = parts[parts.length - 1];
    return filenames.has(filename.toLowerCase());
  });
  return { filteredFiles, allFiles: paths };
};

或者,如果您需要 filteredFiles,并且不包含在 filteredFiles

中的文件
const filterFiles = async (folderPath) => {
  let filename, parts;
  const paths = await getAllFile(folderPath);
  const filteredFiles = [];
  const otherFiles = [];
  for (const filePath of paths) {
    parts = filePath.split("/");
    filename = parts[parts.length - 1];
    if (filenames.has(filename.toLowerCase())) {
      filteredFiles.push(filePath);
    } else {
      otherFiles.push(filePath);
    }
  }
  return { filteredFiles, otherFiles };
};

更新了下面的部分

const fs = require("fs");
const path = require("path");

const getAllFile = async (folderPath) => {
  let files = fs.readdirSync(folderPath);
  files = await Promise.all(
    files.map(async (file) => {
      const filePath = path.join(folderPath, file);
      const stats = fs.statSync(filePath);
      if (stats.isDirectory()) {
        return await getAllFile(filePath);
      } else if (stats.isFile()) return filePath;
    })
  );
  return files.reduce((all, folderContents) => all.concat(folderContents), []);
};

const filenames = new Set([
  "index.html",
  "default.htm",
  // Add your files here which you need to see in filteredFiles and don't need in otherFiles
]);

const filterFiles = async (folderPath) => {
  let filename, parts;
  const paths = await getAllFile(folderPath);
  const filteredFiles = [];
  const otherFiles = [];
  for (const filePath of paths) {
    parts = filePath.split("/");
    filename = parts[parts.length - 1];
    if (filenames.has(filename.toLowerCase())) {
      filteredFiles.push(filePath);
    } else {
      otherFiles.push(filePath);
    }
  }
  return { filteredFiles, otherFiles };
};

filterFiles("./test")
  .then(({ filteredFiles, otherFiles }) => {
    console.log("filteredFiles:::", filteredFiles);
      console.log("otherFiles:::", otherFiles);
  })
  .catch((e) => console.log("ERRROR::", e));