Created
May 11, 2021 15:58
-
-
Save bbody/5ed0766d3705db34683694756b4aac16 to your computer and use it in GitHub Desktop.
Get filenames recursively in a root directory
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 getListOfFiles = function (directory) { | |
let files = []; | |
fs.readdirSync(directory).forEach(function(filename) { | |
const filePath = path.join(directory, filename); | |
const fileStatus = fs.lstatSync(filePath); | |
if (fileStatus.isFile()) { | |
// Check if image | |
if (hasImageExtension(filename)) { | |
files.push(filePath); | |
} | |
} else if (fileStatus.isDirectory()) { // Being specific to avoid non directory and files, e.g. socket, fifo, etc. | |
// Join results with sub directory | |
files = [...files, ...getListOfFiles(filePath)]; | |
} | |
}); | |
return files; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment