Skip to content

Instantly share code, notes, and snippets.

@tomgp
Created November 24, 2015 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomgp/e1847c18a2c44178e25c to your computer and use it in GitHub Desktop.
Save tomgp/e1847c18a2c44178e25c to your computer and use it in GitHub Desktop.
in NodeJS get a list of files, depth first down the directory tree, matching a certain function
var fs = require('fs');
var path = require('path');
function listFileMatches( p, f ){
var fileList = [];
fs.readdirSync(p).forEach(function(d,i){
var fullPath = path.join(p, d);
if (fs.statSync( p + '/' + d ).isDirectory()){
fileList = fileList.concat( listFileMatches(fullPath, f) );
}
if ( f(d) ){
fileList = fileList.concat( fullPath );
}
});
return fileList;
}
//eg. build a list of shapefiles that have names that aren't just '.shp'
console.log( listFileMatches('./', function(d){ return d.indexOf('.shp')>0; }) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment