Reputation: 24059
I use fs.stat to check if a folder exists:
fs.stat('path-to-my-folder', function(err, stat) {
if(err) {
console.log('does not exist');
}
else{
console.log('does exist');
}
});
Is there a way to check the existence of multiple paths using only one method?
Upvotes: 0
Views: 396
Reputation: 26607
fs
doesn't have anything that does it out of the box, but you can create a function to do it.
function checkIfAllExist (paths) {
return Promise.all(
paths.map(function (path) {
return new Promise(function (resolve, reject) {
fs.stat(path, function (err, stat) {
err && reject(path) || resolve()
});
});
}))
);
};
You would use this like this:
checkIfAllExist([path1, path2, path3])
.then(() => console.log('all exist'))
.catch((path) => console.log(path + ' does not exist')
You could tweak it to fail at different points and whatnot, but you get the general idea.
Upvotes: 2
Reputation: 92579
No, the File System API doesn't have a function to check if multiple folders exists. You just have to call the fs.stat()
function multiple times.
Upvotes: 1