Created
June 30, 2014 22:31
-
-
Save jixunmoe/ed31a552982cdb2c20f7 to your computer and use it in GitHub Desktop.
Simple name fixer for anime
This file contains 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
/* | |
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