I have a code which check all the directories and sub-directories so while checking the directories and sub directories i want to check if the specific extension file is present or not if present then i want to filter them in two array
code i have to check all the files is
async function checkFileLoc(folderPath, depth) {
depth -= 1;
let files = await fsPromises.readdir(folderPath);
files = await Promise.all(
files.map(async (file) => {
const filePath = path.join(folderPath, file);
const stats = await fsPromises.stat(filePath);
if (stats.isDirectory() && depth > 0) {
return checkFileLoc(filePath, depth);
} else if (stats.isFile()) return filePath;
else return null;
})
);
return files
.reduce((all, folderContents) => all.concat(folderContents), [])
.filter((e) => e != null);
}
Code to check if the .jpg
ext file present or not but its not working for me and i want to filter further.
async function FilterFile(folderPath) {
wantExt = [".jpg"];
let parts;
const paths = await checkFileLoc(folderPath, 3);
const otherFiles = [];
for (const filePath of paths) {
parts = filePath.split("/");
let splitFileName = parts[parts.length - 1].split(".");
if (wantExt.includes(`.${splitFileName[splitFileName.length - 1]}`)) {
otherFiles.push(filePath);
}
}
return { otherFiles };
}
I want to check if my location contain all the .jpg
file or not if it contain then its should filter _bio.jpg
and other .jpg
files differently.
Output:
bioArr=
[ "animal_bio.jpg"
"mammal_bio.jpg"
]
otherArr=
[ "tree_doc.jpg"
"human.jpg"
"flowes_info.jpg"
]
let bioArray = paths.filter(x => x.endsWith("_bio.jpg"));
paths = paths.filter( x => !bioArray.includes(x)); //removing all bio items
let otherJpg = paths.filter(x => x.endsWith(".jpg"));