【问题标题】:return all the files with filtered files using node js使用节点 js 返回所有带有过滤文件的文件
【发布时间】:2021-04-07 21:52:40
【问题描述】:

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

我有一个代码,它只返回扩展名为 Index.html, Default.htm, Index.aspx 的文件也需要其余文件,但不知道如何返回文件的其余部分以及过滤后的扩展名

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;
};

【问题讨论】:

    标签: node.js async-await promise callback nodes


    【解决方案1】:

    您的filterFiles 可以返回带有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));
    

    【讨论】:

    • @Aakash 如果您只需要在filteredFiles 中有.html 文件,您需要更新filenames 并只保留您需要的值。
    • @Cyber​​Ternal 我已经更新了我想要的filenames,但在otherFiles 我不需要任何.html .html .aspx 文件,它应该在filenames 中完全过滤
    • @Cyber​​Ternal filterFiled 的输出只包含filenamesotherfiles 中提到的文件没有任何.html .htm .aspx 文件??
    • 问题是我在otherFiles 中获取所有其他.html .htm 文件,我不想要otherFilesfilterFiles 中的任何.html、.htm 文件我只想要index.html , default.htm 文件不是任何 abc.html 或 pqr.htm 文件,它应该完全过滤其他 .html 文件,所以我的输出看起来像 filterfile: index.html,default.htmotherFiles: abc.txt, pqr.pdf
    猜你喜欢
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 2015-10-30
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多