
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
filter-files
Advanced tools
Recursively read directories and return a list of files, filtered to have only the files for which the (optional) filter function returns `true`. Sync and async.
Recursively read directories and return a list of files, filtered to have only the files for which the (optional) filter function returns
true. Sync and async.
npm i filter-files --save
npm test
var filter = require('filter-files');
filter.sync(dir, [recurse], [filterFn]);
Params
dir {String}: the directory to start from. Returns all files, recursively, starting with this path.filterFn {Function}: optionally pass a filter function to use for filtering files/dirs. This function filters "on the fly", so recursion is very fast.recurse {Boolean}: pass false to disable recursion.Examples
var files = filter.sync('lib');
console.log(files);
//=> [ 'lib/async.js', 'lib/filter.js', 'lib/sync.js' ]
Pass a filter function:
filter.sync('lib', function(fp) {
return /a/.test(fp);
});
//=> [ 'lib/async.js' ]
Or an array of filter functions:
function include(fp) {
return /^\./.test(fp);
}
function exclude(fp) {
return !/^\.[jntv]/.test(fp);
}
// pass `false` to prevent recursion
filter('.', [include, exclude], false);
//=> ['.git', '.gitignore', '.gitattribuets']
filter(dir, [recurse], [filterFn], callback));
Params
Same as sync with the addition of callback.
Examples
filter('lib', function(err, files) {
console.log(files);
//=> [ 'lib/async.js', 'lib/filter.js', 'lib/sync.js' ]
});
Pass a filter function:
var fn = function(fp) {
return /a/.test(fp);
};
filter('lib', fn, function(err, files) {
console.log(files);
//=> [ 'lib/async.js' ]
});
Filter functions take four parameters and return true or false.
Params
fp filepath being looped overdir current directoryfiles accumulated array of filesrecurse whether recurse is true or falseExample
This function returns a filter function for getting files with the given extname:
var path = require('path');
var isDir = require('is-directory');
function ext(extname) {
// this is our filter function
return function filter(fp, dir, files, recurse) {
if (isDir(path.join(dir, fp)) && recurse === true) {
return true;
}
return path.extname(fp) === extname;
}
}
See the tests for more examples.
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Jon Schlinkert
Copyright (c) 2014 Jon Schlinkert
Released under the MIT license
This file was generated by verb on November 17, 2014.
FAQs
Recursively read directories and return a list of files, filtered to have only the files for which the (optional) filter function returns `true`. Sync and async.
The npm package filter-files receives a total of 3,728 weekly downloads. As such, filter-files popularity was classified as popular.
We found that filter-files demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.