Read a directory of files.
npm install --save read-directory
var read = require('read-directory')
read('./files', function (err, contents) {
console.log(contents)
})
var read = require('read-directory')
var contents = read.sync('./files')
Use the included browserify transform module to convert calls to read.sync
to the contents of the directory.
Note that to use the browserify transform you must use read.sync
, and the path to the file directory can not be a variable.
var path = require('path')
var read = require('read-directory')
var contents = read.sync(path.join(__dirname, 'files'))
browserify index.js -t read-directory -o bundle.js
budo index.js:bundle.js -- -t read-directory
Read the contents of a directory asynchronously
Parameters
-
dir
String – The directory to read -
options
Object-
options.fs
Object – alternate fs implementation, optional -
options.dirnames
Boolean – include or exclude subdirectory names in keys of returned object -
options.encoding
String – encoding of files, default: utf8 -
options.filter
String – glob pattern for filtering files, examples:*.md
,*.css
-
options.ignore
String – glob pattern for ignoring files -
options.ignore
Array – array of glob patterns for ignoring files -
options.transform
Function – A function you can use to transform the contents of files after they are read
-
Examples
var read = require('read-directory')
read('./files', function (err, contents) {
console.log(contents)
})
Read the contents of a directory asynchronously
Parameters
-
dir
String – The directory to read -
options
Object-
options.fs
Object – alternate fs implementation, optional -
options.dirnames
Boolean – include or exclude subdirectory names in keys of returned object -
options.encoding
String – encoding of files, default: utf8 -
options.filter
String – glob pattern for filtering files, examples:*.md
,*.css
-
options.ignore
String – glob pattern for ignoring files -
options.ignore
Array – array of glob patterns for ignoring files -
options.transform
Function – A function you can use to transform the contents of files after they are read
-
Examples
var read = require('read-directory')
var contents = read.sync('./files')