Search code examples
node.jsasync-awaitpromisecallbacknodes

return all the files with filtered files using node js


I want to return file which are present inside folder and sub-folder but need filter on files who's extension ends with .html, .htm or .aspx

I have a code which return only files with extension Index.html, Default.htm, Index.aspx need rest of the files too but don't know how to return rest of the file along with this filtered extension

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

Solution

  • Your filterFiles can return an object with the filteredFiles and allFiles.

    For example

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

    Or, if you need filteredFiles, and files that are not including on 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 };
    };
    

    UPDATED PART BELOW

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