Created
May 9, 2014 04:13
-
-
Save kyeotic/0f35ebdf1701caa9f017 to your computer and use it in GitHub Desktop.
renamer.js
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
require('colors'); | |
var path = require('path'), | |
fs = require('fs'), | |
Q = require('Q'), | |
readDir = Q.denodeify(fs.readdir), | |
stat = Q.denodeify(fs.stat), | |
rename = Q.denodeify(fs.rename); | |
var dirToRead = '$DIRNAME$', | |
regexReplace = '$REGEX$', | |
regexFind = /REPLACEMENT/gi; | |
readDir(dirToRead) | |
.then(function(files) { | |
var promises = files.map(function(file) { | |
//console.log('Found File: ', file.cyan); | |
return stat(path.join(dirToRead, file)) | |
.then(function(stat) { | |
var newPath = file.replace(regexFind, regexReplace); | |
return { isFile: stat.isFile(), oldPath: path.join(dirToRead, file), newPath: path.join(dirToRead, newPath)}; | |
}); | |
}); | |
return Q.allSettled(promises); | |
}).then(function(results) { return results.map(function(r) { return r.value; }); }) | |
.then(function (files) { | |
var promises = files.filter(function(stat) { | |
return stat.isFile; | |
}).map(function(file) { | |
return rename(file.oldPath, file.newPath); | |
}); | |
return Q.allSettled(promises); | |
}) | |
.fail(function(error) { | |
console.log(error.toString().red.bold); | |
}).done(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment