As the title describes, I would like to read all files from a specific directory and return the names of all the files that have a specific string in its contents.
Here is how my code looks so far:
const dirname = path.resolve("./results/");
async function readFiles(dirname) {
const allResults = []
fs.readdir(dirname, function(err, filenames) {
if (err) {
console.log(err);
return;
}
filenames.forEach(async function(filename) {
fs.readFile(dirname + "/" + filename, 'utf-8', function(err, content) {
if (err) {
console.log(err);
return;
}
if (content.includes('content string')) {
allResults.push(filename);
}
});
});
});
return allResults;
}
readFiles(dirname).then((res) => {
console.log(res);
})
The result I'm getting is []
so I understand it's an issue with promises and async functions, however, this is not a concept I fully grasp yet, and despite trying several combinations of possibilities (new Promise()
, or .then
, await
, or readdirSync
and readFileSync
) I had no success.
What am I missing so that it returns the allResults
array only once all files have been read?