【发布时间】:2021-07-02 22:45:06
【问题描述】:
有没有办法在特定点或3级树结构之后停止读取目录
例如
MainFolder
-ABC Folder
-DEF Folder
-GHI Folder
-image.png
-jkl.gif
当我点击 ABC 文件夹时,我的路径将如下所示 /MainFolder/ABC Folder
如果其中有任何其他文件夹或文件,那么它看起来像
/MainFolder/ABC Folder
-PQR Folder
-STU Folder
-abc.pdf
-xyz.txt
点击 PQR 文件夹然后它看起来像这样
/MainFolder/ABC Folder/PQR Folder
-DFC Folder
-HKJ Folder
-mnb.pdf
-xyz.txt
但不应读取 DFC 文件夹/HKJ 文件夹或 3 级树结构后存在的任何其他文件夹 输出:-
["MainFolder/image.png",
"MainFolder/jkl.gif",
"MainFolder/ABC Folder/abc.pdf",
"MainFolder/ABC Folder/xyz.txt",
"MainFolder/ABC Folder/PQR Folder/mnb.pdf",
"MainFolder/ABC Folder/PQR Folder/xyz.txt"]
我让它读取所有文件和子目录的代码,但我想停在 3level 目录
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), []);
}
PS : 使用节点 10.16.3
【问题讨论】:
标签: javascript node.js async-await promise callback