Event based recursive directory reader


Recursive Directory Reader for Node.js

$ npm install 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

Every event listener (except of error) becomes two arguments passed: the file path and the fs.Stat object. The following event names are available:

  • 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

How to include:

    var RecDirReader = require('recdirreader');

Let's start with this simple piece of code, which prints simply all files 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 the following 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 or *.png files from /home/myname/Pictures:

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

RecDirReader emits an event for every file type. So you can listen to the directory, file, symboliclink, blockdevice, characterdevice, fifo, or socket event.

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

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

What if you want to have only symbolic links? The symboliclink event is the key:

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

Or you just listen to the any event, which is emitted by any type of file, inclusively directories:

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