Created
October 9, 2014 20:31
-
-
Save shime/5d0287aa454aaebf69da to your computer and use it in GitHub Desktop.
list files in a directory with node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'), | |
path = require('path') | |
var listFiles = function(dir, next){ | |
fs.readdir(dir, function(err, nodes){ | |
if (err) next(err) | |
next(null, nodes.filter(function(node){ | |
return fs.lstatSync(path.resolve(dir) + "/" + node).isFile() | |
})) | |
}) | |
} | |
// usage: listFiles(dirName) | |
// example: | |
// listFiles('./public/images/', function(err, files){ | |
// if (err) throw err | |
// console.log(files) | |
// }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment