Skip to content

Instantly share code, notes, and snippets.

@tjunghans
Last active September 19, 2016 18:32
Show Gist options
  • Save tjunghans/9c3c03fc37dfa8f5a49e2db56fd412d9 to your computer and use it in GitHub Desktop.
Save tjunghans/9c3c03fc37dfa8f5a49e2db56fd412d9 to your computer and use it in GitHub Desktop.
Reading directory contents using node
const fs = require("fs");
const path = require("path");
const PATH = "."
function listDirs(files) {
files.forEach(file => {
fs.stat(path.join(PATH, file), (err, stats) => {
if (stats.isDirectory()) {
console.log(file);
}
});
});
}
function getFiles(path) {
return new Promise((resolve, reject) => {
fs.readdir(path, {
encoding: "utf-8"
}, (err, files) => {
if (err) {
reject(err);
} else {
resolve(files);
}
});
});
}
getFiles(PATH).then(listDirs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment