Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gentildpinto/3c44fb3ec4c12d95ba1c51c98a690f04 to your computer and use it in GitHub Desktop.
Save gentildpinto/3c44fb3ec4c12d95ba1c51c98a690f04 to your computer and use it in GitHub Desktop.
recursively find all files in a directory with given extension in node.js
var path = require('path')
var fs = require('fs')
function recFindByExt(base,ext,files,result)
{
files = files || fs.readdirSync(base)
result = result || []
files.forEach(
function (file) {
var newbase = path.join(base,file)
if ( fs.statSync(newbase).isDirectory() )
{
result = recFindByExt(newbase,ext,fs.readdirSync(newbase),result)
}
else
{
if ( file.substr(-1*(ext.length+1)) == '.' + ext )
{
result.push(newbase)
}
}
}
)
return result
}
ext_file_list = recFindByExt('/mypath','ext')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment