const fs = require("fs").promises;
const path = require("path");
async function findFiles(folderName) {
const items = await fs.readdir(folderName, { withFileTypes: true });
items.forEach((item) => {
if (path.extname(item.name) === ".json") {
console.log(`Found file: ${item.name} in folder: ${folderName}`);
} else {
findFiles(path.join(folderName, item.name));
}
});
}
findFiles("stores");
Considérons la structure :
$ node index.js
Found file: sales.json in folder: stores\201
Found file: sales.json in folder: stores\203
Found file: sales.json in folder: stores\201\204
Found file: sales.json in folder: stores\201\204\202