Page 1 of 1

Map regex replace on folders only

Posted: 27 Jul 2017, 23:15
by devster
What would be the best way to replace a series of strings in folder names with others but otherwise leave the file untouched?

I was thinking of a csv file with a map of replacements, for example:

Code: Select all

[rarbg];
[ettv];ETTV
...
And such but I'm struggling to find the correct mix of logic and bindings to make this work.

Code: Select all

import java.util.regex.Pattern
def map = csv('path/to/file.csv')
def s = String
map.each{ k, v ->
    def p = Pattern.compile(Pattern.quote(k), Pattern.CASE_INSENSITIVE)
    s = file.getPath().replaceAll(p, v)
}
return s
The above however [should] work on single files and not on the folder while the objective would be to have:

Code: Select all

Big.Hero.6.2014.1080p.BluRay.x264-SPARKS[rarbg]/ -> Big.Hero.6.2014.1080p.BluRay.x264-SPARKS/
An added bonus would be if I could also move said folder to another location and leave a symlink to it in its place with the original name.

Re: Map regex replace on folders only

Posted: 28 Jul 2017, 05:03
by rednoah
1.
Isn't that just generic file mode with a custom format?
viewtopic.php?t=2072

In the format, you have the {folder.name} so you can do whatever you want with that piece of information.


2.
You can use reduce / inject to simplify the code:

Code: Select all

def s = 'abc'
def m = [a:'1', b:'2', c:'3']

m.inject(s) { i, k, v -> i.replaceAll(k, v) }

Re: Map regex replace on folders only

Posted: 05 Aug 2017, 20:09
by devster
I'm trying to do what you suggested, however there are a couple of quirks.
The first one is that the folder to be renamed might contain additional folders, therefore the following 2 commands have different outputs:

Code: Select all

filebot --action test -rename $folder_name --format $format --output $media_output
This ignores subfolders such as the Subs folder.

Code: Select all

filebot -r --action test -rename $folder_name --format $format --output $media_output
This does not ignore those folders but this happens:

Code: Select all

/some/directory/$folder_name/Subs/$subname -> $media_output/Subs/$subname
On the bright side the format seems to work fine with your suggestion to use inject.

I feel maybe filebot isn't really necessary for this.

Re: Map regex replace on folders only

Posted: 06 Aug 2017, 03:05
by rednoah
You'd have to express that logic in your format as well:

Code: Select all

if folder is sub folder, then use parent folder, otherwise use folder