HattrickNZ
HattrickNZ

Reputation: 4663

node.js fs.readdir recursive directory search [2]

I can't get the serial example from here but I can't provide the link(?):

NodeJS: How would one watch a large amount of files/folders on the server side for updates?

var fs = require('fs');
var walk = function(dir, done) {
  var results = [];
  fs.readdir(dir, function(err, list) {
    if (err) return done(err);
    var i = 0;
    (function next() {
      var file = list[i++];
      if (!file) return done(null, results);
      file = dir + '/' + file;
      fs.stat(file, function(err, stat) {
        if (stat && stat.isDirectory()) {
          walk(file, function(err, res) {
            results = results.concat(res);
            next();
          });
        } else {
          results.push(file);
          next();
        }
      });
    })();
  });
};

I have tried this to call the function but I am not sure what parameters it needs. Can someone please advise?

//walk(process.env.HOME, function(err, results) {
walk("C:\", function(err, results) {
  if (err) throw err;
  console.log(results);
});

the error I'm getting is

module.js:437
  var compiledWrapper = runInThisContext(wrapper, filename, true);
                        ^
SyntaxError: Unexpected token ILLEGAL
    at Module._compile (module.js:437:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.runMain (module.js:492:10)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

Upvotes: 0

Views: 1089

Answers (1)

Dan D.
Dan D.

Reputation: 74685

Escape your \. Use "C:\\" not "C:\".

Upvotes: 1

Related Questions