0

I want to check folder with names which are present in array and only select them which are present inside array list but with if condition and return the value which are present inside FileArray

let extensionArray = [".html", ".htm", ".aspx"];
  let fileArray = [
    "index.html",
    "index.htm",
    "index.aspx",
    "Index.html",
    "Index.htm",
    "Index.aspx",
    "default.html",
    "default.htm",
    "default.aspx",
    "Default.html",
    "Default.htm",
    "Default.aspx",
  ];

if(!extensionArray.include(true){
if(!fileArray.inclue(true){
// return the output 
}
}

I have checked one of the post in which file can be checked from all the folder and subfolder

but I don't know where to apply my condition to check the extension and file name and then return it.

code is as follow :-

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

async function getFile(dir) {
  let files = await fs.readdir(dir);
  files = await Promise.all(
    files.map(async (file) => {
      const filePath = path.join(dir, file);
      const stats = await fs.stat(filePath);
      if (stats.isDirectory()) return getFile(filePath);
      else if (stats.isFile()) return filePath;
    })
  );

  return files.reduce((all, folderContents) => all.concat(folderContents), []);
}
1
  • yes @ArunKumarMohan Commented Apr 5, 2021 at 8:44

1 Answer 1

2

You don't have to add the filenames in both capitalized and lowercase forms to fileArray. You can convert the filenames to lowercase when filtering them. And you can add the filenames to a Set. Also, you don't need extensionArray since you're going to check for the filenames directly. Once you have the list of file paths in the directory by calling the getFilePaths function, you can filter them by checking if the lowercased filename (obtained by splitting the file path by / and getting the last element in the array) is present in the set.

const fs = require('fs').promises
const path = require('path')

const filenames = new Set([
  'index.html',
  'index.htm',
  'index.aspx',
  'default.html',
  'default.htm',
  'default.aspx',
])

const getFilePaths = async (dir) => {
  let files = await fs.readdir(dir)
  files = await Promise.all(
    files.map(async (file) => {
      const filePath = path.join(dir, file)
      const stats = await fs.stat(filePath)
      if (stats.isDirectory()) {
        return getFilePaths(filePath)
      }
      return filePath
    })
  )

  return files.flat()
}

const filterFiles = async (dir) => {
  const paths = await getFilePaths(dir)
  const filteredFiles = paths.filter((filePath) => {
    const parts = filePath.split('/')
    const filename = parts[parts.length - 1]
    return filenames.has(filename.toLowerCase())
  })
  console.log(filteredFiles)
}

filterFiles('.')
Sign up to request clarification or add additional context in comments.

6 Comments

hey @arun how can i get all the other file (.png,.txt, .pdf) plus file which are present in filename Array (.html, .htm, .aspx) any idea ?
@Aakash You can add another condition in the callback passed to the filter method to check if the file extension (you can use path.extname) is present in the list.
hey @arun sorry for the trouble but can this be possible?? let extName = [".htm",".htm,".aspx"] if(extName){ // then return fileNames Array + other extension file too } else{ other extension file } how can i get this please help
@Aakash I'm not sure if I understand. extName is an array and so is truthy — the if block will always get executed. Can you explain what you're trying to do?
i want to check all my folder with extension .html, .htm or .aspx is present or not if files with these extension are present then i want to filter out files who's name start with index.htm, index.html and return this files along with other extension files which can be any thing it can be .png, .doc, .txt, .pdf, etc. @arun
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.