Skip to content

Instantly share code, notes, and snippets.

@Francis9817
Created August 19, 2020 18:25
Show Gist options
  • Save Francis9817/df5ac08861ad0b9c8adecc160463964b to your computer and use it in GitHub Desktop.
Save Francis9817/df5ac08861ad0b9c8adecc160463964b to your computer and use it in GitHub Desktop.
Ejercicio: Trabajar con el sistema de archivos
const fs = require("fs");
async function findSalesFiles(folderName) {
// this array will hold sales files as they are found
let salesFiles = [];
async function findFiles(folderName) {
// read all the items in the current folder
const items = await fs.readdirSync(folderName, { withFileTypes: true });
// iterate over each found item
for (item of items) {
// if the item is a directory, it will need to be searched for files
if (item.isDirectory()) {
// search this directory for files (this is recursion!)
await findFiles(`${folderName}/${item.name}`);
} else {
// Make sure the discovered file is a sales.json file
if (item.name === "sales.json") {
// store the file path in the salesFiles array
salesFiles.push(`${folderName}/${item.name}`);
}
}
}
}
// find the sales files
await findFiles(folderName);
// return the array of found file paths
return salesFiles;
}
async function main() {
const salesFiles = await findSalesFiles("stores");
console.log(salesFiles);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment