Skip to content

Commit

Permalink
* update replace script for v4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rednoah committed Apr 21, 2014
1 parent 0006cb3 commit ac8ef22
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions replace.groovy
@@ -1,25 +1,14 @@
// filebot -script fn:replace --action copy --filter "[.]srt$" --def "e=[.](eng|english)" "r=.en"
// filebot -script fn:replace --action test --filter "[.]srt$" --def "e=[.](eng|english)" "r=.en"


// parameters
def action = StandardRenameAction.forName(_args.action)
def accept = { f -> _args.filter ? f.path =~ _args.filter : true }
def pattern = Pattern.compile(e, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE)

def renamePlan = [:]

// rename
args.getFiles{ accept(it) }.each{
if (it.path =~ e) {
def nfile = new File(it.path.replaceAll(e, r))

// override files only when --conflict override is set
if (!it.equals(nfile)) {
if (nfile.exists() && _args.conflict == 'override' && action != StandardRenameAction.TEST) {
nfile.delete() // resolve conflict
}

if (!nfile.exists()) {
println action.rename(it, nfile)
} else {
println "Skipped $nfile"
}
}
}
args.getFiles{ f -> accept(f) && pattern.matcher(f.path).find() }.each{ f ->
renamePlan[f] = pattern.matcher(f.path).replaceAll(r) as File
}

rename(map:renamePlan)

0 comments on commit ac8ef22

Please sign in to comment.