使用节点js返回所有包含过滤文件的文件 [英] return all the files with filtered files using node js
问题描述
我想返回文件夹和子文件夹中存在的文件,但需要对扩展名为.html,.htm或.aspx的文件进行过滤
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
我有一个代码,该代码仅返回扩展名为 Index.html,Default.htm,Index.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;
};
推荐答案
您的 filterFiles
可以返回带有 filteredFiles
和 allFiles
的对象.
Your filterFiles
can return an object with the filteredFiles
and allFiles
.
例如
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
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 };
};
下面更新的部分
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));
这篇关于使用节点js返回所有包含过滤文件的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!