Last active
July 18, 2016 09:07
-
-
Save getflourish/955f903de5982fefd6a35c8b84e617b5 to your computer and use it in GitHub Desktop.
Move images from a folder to subfolders named after each filename
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 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