Skip to content

Instantly share code, notes, and snippets.

@awongh
Forked from victorsollozzo/gist:4134793
Last active November 3, 2020 13:27
Show Gist options
  • Save awongh/394d41f845f7f81f3b27a5cd36dccbd9 to your computer and use it in GitHub Desktop.
Save awongh/394d41f845f7f81f3b27a5cd36dccbd9 to your computer and use it in GitHub Desktop.
recursively find all files in a directory with given extension in node.js
import path from 'path';
import { readdirSync, statSync } from 'fs';
function recFindByExt(base,ext,files,result)
{
files = files || readdirSync(base)
result = result || []
files.forEach(
function (file) {
var newbase = path.join(base,file)
if ( statSync(newbase).isDirectory() )
{
result = recFindByExt(newbase,ext,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