Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miguelmota/31284d965e7e88dfec9f to your computer and use it in GitHub Desktop.
Save miguelmota/31284d965e7e88dfec9f to your computer and use it in GitHub Desktop.
Node.js read directory files and write to JSON file
var fs = require('fs');
fs.readdir([__dirname,'/app/images'].join(''), function(err, files) {
function filter(file) {
return /(\.png|\.jpg|\.jpeg)/.test(file);
}
var filtered = JSON.stringify(files.filter(filter));
fs.writeFile([__dirname,'/app/data/images.json'].join(''), filtered, function(err) {
if (err) throw new Error(err);
console.log(filtered);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment