Skip to content

Instantly share code, notes, and snippets.

@cgkio
Created September 23, 2013 14:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cgkio/6671662 to your computer and use it in GitHub Desktop.
Save cgkio/6671662 to your computer and use it in GitHub Desktop.
Getting a directory listing using the fs module in Node.js
#!/usr/bin/env node
var fs = require("fs"),
path = require("path");
var p = "../"
fs.readdir(p, function (err, files) {
if (err) {
throw err;
}
files.map(function (file) {
return path.join(p, file);
}).filter(function (file) {
return fs.statSync(file).isFile();
}).forEach(function (file) {
console.log("%s (%s)", file, path.extname(file));
});
});
//From http://nodeexamples.com/2012/09/28/getting-a-directory-listing-using-the-fs-module-in-node-js/
//The previous example grabs a directory listing of the Node.js script’s parent directory (../) using the asynchronous fs.readdir() method. If no errors were encountered, the event object’s files property will contain an array of file and folder names for the specified directory.
//Next, we use the Array.map() method to join the directory name and the current file name using the Node.js path.join() method.
//Then we use the Array.filter() method to filter out only the files using the fs module’s statSync() method and the fs.Stats class’ isFile() method.
//Finally, we use the Array.forEach() method to iterate over each non-filtered item in the files array and display the filename and extension (using the path.extname() method).
//We could also easily use the Array.filter() method to filter the file array by the file extension if we only wanted to interate over .js/.json files or .png files, or instead we could use the fs.stats class’ isDirectory() method to recursively get the nested directory structure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment