Skip to content

Instantly share code, notes, and snippets.

@kyeotic
Created May 9, 2014 04:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyeotic/0f35ebdf1701caa9f017 to your computer and use it in GitHub Desktop.
Save kyeotic/0f35ebdf1701caa9f017 to your computer and use it in GitHub Desktop.
renamer.js
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