Created
November 24, 2015 17:33
-
-
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
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
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