A lightweight Node.js module with methods for some common directory and file operations, including asynchronous, non-blocking methods for recursively getting an array of files, subdirectories, or both, and methods for recursively, sequentially reading and processing the contents of files in a directory and its subdirectories, with several options available for added flexibility if needed.
Sequentially read the content of each file in a directory, passing the contents to a callback, optionally calling a finished callback when complete. The options and finishedCallback arguments are not required.
Valid options are:
encoding: file encoding (defaults to 'utf8')
exclude: a regex pattern or array to specify filenames to ignore
excludeDir: a regex pattern or array to specify directories to ignore
match: a regex pattern or array to specify filenames to operate on
matchDir: a regex pattern or array to specify directories to recurse
recursive: whether to recurse subdirectories when reading files (defaults to true)
reverse: sort files in each directory in descending order
shortName: whether to aggregate only the base filename rather than the full filepath
sort: sort files in each directory in ascending order (defaults to true)
doneOnErr: control if done function called on error (defaults to true)
A reverse sort can also be achieved by setting the sort option to 'reverse', 'desc', or 'descending' string value.
readFilesStream examples
// display contents of files in this script's directorydir.readFiles(__dirname, function(err, content, next) { if (err) throw err; console.log('content:', content); next(); }, function(err, files){ if (err) throw err; console.log('finished reading files:', files); });// display contents of huge files in this script's directorydir.readFilesStream(__dirname, function(err, stream, next) { if (err) throw err; var content = ''; stream.on(
files( dir, callback )
Asynchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback.
dir.files(__dirname, function(err, files) { if (err) throw err; console.log(files);});
files( dir, {sync:true} )
Synchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback.
var files = dir.files(__dirname, {sync:true});console.log(files);
promiseFiles( dir, callback )
Asynchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback.
Note that for the files and subdirs the object returned is an array, and thus all of the standard array methods are available for use in your callback for operations like filters or sorting. Some quick examples:
dir.files(__dirname, function(err, files) { if (err) throw err; // sort ascending files.sort(); // sort descending files.reverse(); // include only certain filenames files = files.filter(function (file) { return ['allowed', 'file', 'names'].indexOf(file) > -1; }); // exclude some filenames files = files.filter(function (file) { return ['exclude', 'these', 'files'].
Also note that if you need to work with the contents of the files asynchronously, please use the readFiles method. The files and subdirs methods are for getting a list of the files or subdirs in a directory as an array.
subdirs( dir, callback )
Asynchronously iterate the subdirectories of a directory and its subdirectories and pass an array of directory paths to a callback.
dir.subdirs(__dirname, function(err, subdirs) { if (err) throw err; console.log(subdirs);});
paths(dir, [combine], callback )
Asynchronously iterate the subdirectories of a directory and its subdirectories and pass an array of both file and directory paths to a callback.
Separated into two distinct arrays (paths.files and paths.dirs)