Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 26th, 2012  |  syntax: None  |  size: 0.88 KB  |  views: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var fs = require("fs");
  2. var path = require("path");
  3.  
  4. var walk = function (start, callback) {
  5.   fs.lstat(start, function (err, stat) {
  6.     if (err) return callback(err)
  7.  
  8.     if (stat.isDirectory() && !stat.isSymbolicLink()) {
  9.       fs.readdir(start, function (err, files) {
  10.         if (err) return callback(err)
  11.         var dirs = []
  12.           , notDirs = []
  13.           , IL = files.length
  14.  
  15.         for (i = 0; i < IL; i++) {
  16.           var abspath = path.join(start, files[i]);
  17.           var _stat = fs.lstatSync(abspath);
  18.  
  19.           if (_stat.isDirectory() && !_stat.isSymbolicLink()) {
  20.             dirs.push(abspath);
  21.             walk(abspath, callback);
  22.           } else {
  23.             notDirs.push(abspath);
  24.           }
  25.         }
  26.  
  27.         return callback(null, start, dirs, notDirs);
  28.       });
  29.     } else {
  30.       return callback(new Error("path: " + start + " is not a directory"));
  31.     }
  32.   });
  33. };
clone this paste RAW Paste Data