Event based recursive directory reader


Recursive Directory Reader for Node.js

$ npm install recdirreader
    var RecDirReader = require('recdirreader');
RecDirReader ( dir: Array ): RecDirReader
RecDirReader ( dir: String ): RecDirReader
RecDirReader ( dir: Array, filter: RegExp ): RecDirReader
RecDirReader ( dir: String, filter: RegExp ): RecDirReader
RecDirReader.dir ( dir: Array ): RecDirReader
RecDirReader.dir ( dir: String ): RecDirReader
RecDirReader.filter ( filter: RegExp ): RecDirReader
  • directory for directories
  • file for files
  • symboliclink for symlinks
  • blockdevice for block devices
  • characterdevice for character devices
  • fifo for named pipes
  • socket for sockets
  • any for any of above event
  • empty for empty directories

Every event listener (except of error) becomes two arguments passed: the file path and its fs.Stat object.

Let's start with this simple piece of code, which simply prints all files (not directories) from /home/myname:

    RecDirReader('/home/myname').on('file', function(filestat) {
      // print file path and size 
      console.log('File: ' + file + ' (' + stat.size + ')');

Alternatively you can pass the directory /home/myname directly to the scan() method:

    RecDirReader().on('file', function(filestat) {
      console.log('File: ' + file + ' (' + stat.size + ')');

As you can see: both, RecDirReader() and its scan() method accept a directory string as first argument. But you can also pass an Array with Strings. Like in this example:

    RecDirReader(['/home/myname/Pictures', '/home/myname/Videos'])
      .on('file', function(file) {
        console.log('File:', file);

How about filters? Let's say you want to have only *.jpg and/or *.png files from /home/myname/Pictures:

    RecDirReader('/home/myname/Pictures',  /\.(jpg|png)$/).on('file', function(filestat) {
      console.log('Picture found:', file);

If you want to have only directories, listen to the directory event:

    RecDirReader().on('directory', function(file) {
      console.log('Directory found:', file);

Get only symbolic links:

    RecDirReader().on('symboliclink', function(file) {
      console.log('Symlink found:', file);

Get all empty directories:

    var emptyDirectories = [];
    RecDirReader().on('empty', function(dir) {
    }).on('end', function() {
      console.log('Empty Directories:', emptyDirectories);

Or just listen to the any event to capture any file/directory:

    RecDirReader().on('any', function(file) {