I'm still new to Javascript and Nodejs and I've been trying to access different files inside different child folders of a common parent. Here's what I mean by that
.
├── _captures (parent folder)
├── _folder1 (child 1)
│ ├── file1.png
│ └── file2.png
├── _folder2 (child 2)
│ ├── file1.png
So I did a for loop that iterates over each child folder and I used a nested fs.readdir which would iterate over each file in the folder.
The problem is that the fs.readdir gets called only after all the iterations and keeps looping on the last folder only.
For example if directories.length is equal to 50 it would loop over the last folder 50 times. Here's the script
var Directories = getDirectories('.\\captures\\');
for (i = 0; i < Directories.length; i++) {
var dirtest = '.\\VisualTests\\' + Directories[i];
var dirref = '.\\captures\\' + Directories[i];
if (!fs.existsSync(dirtest)) {
fs.mkdirSync(dirtest);
}
fs.readdir(dirref, function (err, files) {
console.log(dirref);
if (err) {
console.error("Could not list the directory.", err);
process.exit(1);
}
if (files.length == 0) {
console.log("test skipped or pending");
}
else {
files.forEach(function (file) {
console.log(file);
});
}
});
}
How can I make this work ?