Skip to content

Instantly share code, notes, and snippets.

@jixunmoe

jixunmoe/fixName.js

Created Jun 30, 2014
Embed
What would you like to do?
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
You can’t perform that action at this time.