I'm trying to read through a directory structure and sub-directories to find a file with a name that matches a set of criteria. I'm using a recursive function to walk through each item in a directory. If its a file and matches the criteria, return the file path, otherwise, move on to the next item.
I can log the match when I find it, but getting UNDEFINED when trying to return from the function.
Many SO posts like here and here indicate I need to add a return statement to the recursion call but I can't seem to get it right.
How can I return from this function?
function checkForProjectTrackFile(projfolder, projname) {
fs.readdirSync(projfolder).forEach(projfile => {
if (fs.statSync(path.join(projfolder, projfile)).isDirectory()) {
//if a directory, call function again
return checkForProjectTrackFile(path.join(projfolder, projfile), projname)
} else {
if (isProjectTrackMatch(projfile, projname)) {
console.log("Match Found", path.join(projfolder, projfile))
//match criteria met, return the file path => UNDEFINED
return (path.join(projfolder, projfile))
}
}
})
}