Skip to content

Instantly share code, notes, and snippets.

@jixunmoe
Created June 30, 2014 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jixunmoe/ed31a552982cdb2c20f7 to your computer and use it in GitHub Desktop.
Save jixunmoe/ed31a552982cdb2c20f7 to your computer and use it in GitHub Desktop.
Simple name fixer for anime
/*
Usage:
node fixName.js
--dir Dir to search, or `pwd`
--rule Custom Search RegExp Rule
--mod RegExp Modifier, default to i;
--replace What to replace?
--doRename Comfirm to rename
*/
var fs = require ('fs'),
path = require ('path');
var args = {}, argv = {};
for (var i=2, curFlag; i<process.argv.length; i++) {
if (!process.argv[i].indexOf ('--')) {
curFlag = process.argv[i].slice(2);
args[curFlag] = args[curFlag] || [];
} else if (curFlag && args[curFlag]) {
args[curFlag].push (process.argv[i]);
}
}
// Join args
for (var x in args) {
if (args.hasOwnProperty(x)) {
argv[x] = args[x].join(' ');
}
}
/*
if (!args.rule || !args.replace) {
console.error ('Argument missing: rule or replace');
process.exit (1);
}
*/
var regRule = new RegExp (args.rule || "(\\[[a-z\\-&]+\\])(.+?)(\\[\\d+\\])", args.mod || 'i');
// regRule.global = true;
var replace = args.replace || '$3$2$1';
console.log ('SnR: ' + regRule.toString () + ' => ' + replace);
var absPath = path.resolve (args.dir || '.') + '/';
var onlyTest = !args.doRename;
fs.readdirSync (absPath).filter (function (e) {
return regRule.test (e);
}).forEach (function (e) {
console.log (e + '\n => ' + e.replace(regRule, replace));
if (!onlyTest)
fs.renameSync (absPath + e, absPath + e.replace(regRule, replace));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment