Grofit Grofit - 4 months ago 39
Node.js Question

Get all directories within directory nodejs

I was hoping this would be a simple thing, but I cannot find anything out there to do so.

I just want to get all folders/directories within a given folder/directory.

So for example:

<MyFolder>
|- SomeFolder
|- SomeOtherFolder
|- SomeFile.txt
|- SomeOtherFile.txt
|- x-directory


I would expect to get an array of:

["SomeFolder", "SomeOtherFolder", "x-directory"]


Or the above with the path if that was how it was served...

So does anything already exist to do the above?

Answer

Here's a shorter, syncronous version of this answer that can list all directories (hidden or not) in the current directory:

var fs = require('fs'),
    path = require('path');

function getDirectories(srcpath) {
  return fs.readdirSync(srcpath).filter(function(file) {
    return fs.statSync(path.join(srcpath, file)).isDirectory();
  });
}