使用节点js检查文件夹路径时过滤扩展名
filter extension while checking folder path using node js
我有一个检查所有目录和子目录的代码,所以在检查目录和子目录时,我想检查特定的扩展文件是否存在,如果存在,那么我想在两个数组中过滤它们
我必须检查所有文件的代码是
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);
}
用于检查 .jpg
ext 文件是否存在的代码,但它对我不起作用,我想进一步过滤。
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 };
}
我想检查我的位置是否包含所有 .jpg
文件,如果包含则它应该以不同方式过滤 _bio.jpg
和其他 .jpg
文件。
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"));
我有一个检查所有目录和子目录的代码,所以在检查目录和子目录时,我想检查特定的扩展文件是否存在,如果存在,那么我想在两个数组中过滤它们
我必须检查所有文件的代码是
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);
}
用于检查 .jpg
ext 文件是否存在的代码,但它对我不起作用,我想进一步过滤。
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 };
}
我想检查我的位置是否包含所有 .jpg
文件,如果包含则它应该以不同方式过滤 _bio.jpg
和其他 .jpg
文件。
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"));