Created
August 19, 2020 18:25
-
-
Save Francis9817/df5ac08861ad0b9c8adecc160463964b to your computer and use it in GitHub Desktop.
Ejercicio: Trabajar con el sistema de archivos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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