Skip to content

Instantly share code, notes, and snippets.

@joemccann
Created January 14, 2011 21:17
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save joemccann/780256 to your computer and use it in GitHub Desktop.
Save joemccann/780256 to your computer and use it in GitHub Desktop.
Scan a directory and create a JSON object of images (and write it to a file) of all the images in that directory.
var sys = require("sys"),
fs = require("fs");
function processImageDir(path, outFilename, cb) {
fs.readdir(path, function(err, files) {
var imgfiles = [];
// Check for images and push on the array if it's a match.
files.forEach(function(name) {
name.substr(-4).match(/(png|jpeg|jpg|gif)/) && imgfiles.push(name)
});
fs.writeFile(__dirname + '/' + outFilename, JSON.stringify({
images: imgfiles
}), function(err) {
if (err) throw err;
cb && cb("Sweet.");
});
});
}
processImageDir('/path/to/your/images/dir', "preload-images.json", function(message) {
console.log(message);
});
@landed1
Copy link

landed1 commented Jul 10, 2015

I get a parse error on line 4 - I am guessing I am missing some module...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment