Skip to content

Instantly share code, notes, and snippets.

@blogui91
Forked from AndrewRayCode/gist:825583
Created June 14, 2014 06:14

Revisions

  1. @AndrewRayCode AndrewRayCode renamed this gist Feb 14, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @AndrewRayCode AndrewRayCode created this gist Feb 14, 2011.
    42 changes: 42 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    function readDir(start, callback) {
    // Use lstat to resolve symlink if we are passed a symlink
    fs.lstat(start, function(err, stat) {
    if(err) {
    return callback(err);
    }
    var found = {dirs: [], files: []},
    total = 0,
    processed = 0;
    function isDir(abspath) {
    fs.stat(abspath, function(err, stat) {
    if(stat.isDirectory()) {
    found.dirs.push(abspath);
    // If we found a directory, recurse!
    readDir(abspath, function(err, data) {
    found.dirs = found.dirs.concat(data.dirs);
    found.files = found.files.concat(data.files);
    if(++processed == total) {
    callback(null, found);
    }
    });
    } else {
    found.files.push(abspath);
    if(++processed == total) {
    callback(null, found);
    }
    }
    });
    }
    // Read through all the files in this directory
    if(stat.isDirectory()) {
    fs.readdir(start, function (err, files) {
    total = files.length;
    for(var x=0, l=files.length; x<l; x++) {
    isDir(path.join(start, files[x]));
    }
    });
    } else {
    return callback(new Error("path: " + start + " is not a directory"));
    }
    });
    };