class method FsTools.walk
FsTools.walk(path, pattern, iterator[, callback]) → void
FsTools.walk(path, iterator[, callback]) → void
Walks throught all files withing path
(including sub-dirs) and calls
iterator
on each found file (or block device etc.) matching pattern
.
If no pattern
was given - will fire call iterator
for every single
path found. After all iterations will call callback
(if it was specified)
with passing error
as first arguemtn if there was an error.
Iterator
All iterations are running within promise. So callback
given to the walk
will fire only after all iterator
callbacks willnotify they finished their
work:
var iterator = function (path, stats, callback) {
// ... do something
if (err) {
// ... if error occured we can "stop" walker
callback(err);
return;
}
// ... if everything is good and finished notify walker we're done
callback();
};
Iterator is called with following arguments:
path
(String): Full path of the found element (e.g./foo/bar.txt
)stats
(fs.Stats): Stats object of found pathcallback
(Function): Callback function to call after path processing
Example
fstools.walk('/home/nodeca', function (path, stats, callback) {
if (stats.isBlockDevice()) {
callback(Error("WTF? Block devices are not expetcted in my room"));
return;
}
if (stats.isSocket()) {
console.log("Finally I found my socket");
}
callback();
}, function (err) {
if (err) {
// shit happens!
console.error(err);
process.exit(1);
}
console.log("Hooray! We're done!");
});
Example (using pattern matching)
fstools.walk('/home/nodeca', '\.yml$', function (path, stats, callback) {
fs.readFile(path, 'utf-8', funtion (err, str) {
if (err) {
callback(err);
return;
}
console.log(str);
callback();
});
}, function (err) {
if (err) {
console.error(err);
}
console.log('Done!');
});