fs-walk.js/README.md

102 lines
2.3 KiB
Markdown
Raw Normal View History

2010-11-21 05:02:53 +00:00
node-walk
====
A not-quite-port of python's `os.walk`.
Installation
----
npm install walk
Usage
====
All of the examples in the folder included in this repository work and have no typos.
without emitter
----
var walk = require('walk').walk,
2010-11-21 05:02:53 +00:00
options = {
followLinks: false,
2010-11-21 05:02:53 +00:00
};
function fileHandler(err, path, errors, dirs, files, links, blocks, chars, fifos, sockets) {
2010-11-21 05:02:53 +00:00
// handle each path
}
walk("path/to/dir", options, fileHandler);
// This also works
// walk("path/to/dir", options).whenever(fileHandler);
Single Arguments
* `err` - Error when reading path (Probably due to permissions).
* `path` - the current path being read
Array Arguments
* `errors` - `fs.stat` encountered on files in the directory
* `dirs` - directories (modification of this array - sorting, removing, etc - affects traversal)
* `files` - regular files (includes links when `followLinks` is `true`)
* `links` - symbolic links (always empty when `followLinks` is `true`)
* `blocks` - block devices
* `chars` - character devices
* `fifos` - FIFOs
* `sockets` - sockets
using emitter
----
`errors`, `directories`, `files`, `symbolicLinks`
var walk = require('walk').walk,
emitter = walk('./walk-test');
emitter.on("directories", function (path, dirs) {
// the second directory will not be traversed
dirs.splice(1,1);
dirs.forEach(function (dir) {
console.log(dir);
});
});
emitter.on("files", function (path, files) {
files.forEach(function (dir) {
console.log(dir);
});
});
2010-11-21 05:02:53 +00:00
Example
====
mkdir -p walk-test/dir1
touch walk-test/file-1
touch walk-test/file-2
touch walk-test/dir1/file-1
touch walk-test/dir1/file-2
node-walk-test
----
var walk = require('walk');
walk('./walk-test', undefined, function (err, path, errors, dirs, files, links) {
2010-11-21 05:02:53 +00:00
if (err) {
console.log('ERROR: ');
console.log(err);
return;
}
dirs.forEach(function (item, i, arr) {
2010-11-21 05:02:53 +00:00
if (item.name.match(/trash/i)) {
console.log('found a trash');
arr.splice(i,1);
}
});
2010-11-21 05:02:53 +00:00
console.log("PATH: " + path);
console.log("SORTED: ");
console.log(errors, dirs, files, links);
2010-11-21 05:02:53 +00:00
});