Skip to content

Instantly share code, notes, and snippets.

@getflourish
Last active July 18, 2016 09:07
Show Gist options
  • Select an option

  • Save getflourish/955f903de5982fefd6a35c8b84e617b5 to your computer and use it in GitHub Desktop.

Select an option

Save getflourish/955f903de5982fefd6a35c8b84e617b5 to your computer and use it in GitHub Desktop.
Move images from a folder to subfolders named after each filename
var fs = require('fs'),
path = require('path');
var mkdirSync = function (path) {
try {
fs.mkdirSync(path);
} catch(e) {
if ( e.code != 'EEXIST' ) throw e;
}
}
fs.readdir(__dirname, function (error, files) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
// check if file is a png
if (path.extname(file) == ".png") {
var filename = file.substr(0, file.indexOf("."));
console.log("rename " + file + " to " + filename + "/" + "mockup.png")
// create folder based on file name
mkdirSync(filename);
// move the image to the new folder and rename it
fs.rename(file, filename + "/" + "mockup.png")
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment