asynchronous file and directory operations for Node.js
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.
npm install node-dir
For the sake of brevity, assume that the following line of code precedes all of the examples.
var dir = require'node-dir';
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:
A reverse sort can also be achieved by setting the sort option to 'reverse', 'desc', or 'descending' string value.
// display contents of files in this script's directorydirreadFiles__dirnameif err throw err;console.log'content:' content;next;if err throw err;console.log'finished reading files:' files;;// display contents of huge files in this script's directorydirreadFilesStream__dirnameif err throw err;var content = '';streamon'data'content += buffertoString;;streamon'end'console.log'content:' content;next;;if err throw err;console.log'finished reading files:' files;;// match only filenames with a .txt extension and that don't start with a `.´dirreadFiles__dirnamematch: /.txt$/exclude: /^\./if err throw err;console.log'content:' content;next;if err throw err;console.log'finished reading files:'files;;// exclude an array of subdirectory namesdirreadFiles__dirnameexclude: 'node_modules' 'test'if err throw err;console.log'content:' content;next;if err throw err;console.log'finished reading files:'files;;// the callback for each file can optionally have a filename argument as its 3rd parameter// and the finishedCallback argument is optional, e.g.dirreadFiles__dirnameconsole.log'processing content of file' filename;next;;
Asynchronously iterate the files of a directory and its subdirectories and pass an array of file paths to a callback.
dirfiles__dirnameif err throw err;console.logfiles;;
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:
dirfiles__dirnameif err throw err;// sort ascendingfilessort;// sort descendingfilesreverse;// include only certain filenamesfiles = filesfilterreturn 'allowed' 'file' 'names'indexOffile > -1;;// exclude some filenamesfiles = filesfilterreturn 'exclude' 'these' 'files'indexOffile === -1;;;
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.
Asynchronously iterate the subdirectories of a directory and its subdirectories and pass an array of directory paths to a callback.
dirsubdirs__dirnameif err throw err;console.logsubdirs;;
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)
dirpaths__dirnameif err throw err;console.log'files:\n'pathsfiles;console.log'subdirs:\n' pathsdirs;;
Combined in a single array (convenience method for concatenation of the above)
dirpaths__dirname trueif err throw err;console.log'paths:\n'paths;;
MIT licensed (See LICENSE.txt)