Stream file names from a given directory.


Simple stream wrapper for fs.readdir(). Takes a path and streams the contents of the target directory.


Install with NPM:

npm install dirstream


To get a stream of file names from a target directory just do:

var path  = 'path/to/folder/to/scan';
var files = require('dirstream')(path);
files.on('data', function (file) {
files.on('end', function () {
  console.log('All files logged! Fish: ><(( v ) ">');

To get a stream of only the files that match a given pattern do:

var path  = 'path/to/folder/to/scan';
var pattern = /\.jpg$/; // Only give me jpgs
var files = require('dirstream')(path, pattern);
files.on('data', function (jpgname) {
files.on('count', function (count) {
  console.log('Hang on for a stream of ' + count + ' files!');

If you want absolute paths instead of only the file name from the data events you can pass a boolean flag to the constructor. Like so:

var path = ...;
var files = require('dirstream')(path, true);

// Works with filters too
var files = require('dirstream')(path, true, /.\.log$/);


DirStream will emit a count event after the results from fs.readdir() has been counted. Files excluded by filter will not be included in this count.

The data event is emitted once for every file in the target directory.

The end event will be emitted once all files have been emitted.

If DirStream encounters an error, the error event will be emitted.


DirStream has full support for pausing and resuming. If for example you need to load and parse the files emitted but only want to do process a few files at a time. You could implement an internal counter and pause the stream using the .pause() function on the stream once you're processing x number of files. Then call .resume() once a file has completed parsing. Or something.