Map regex replace on folders only

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
devster
Posts: 417
Joined: 06 Jun 2017, 22:56

Map regex replace on folders only

Post 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.
I only work in black and sometimes very, very dark grey. (Batman)
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Map regex replace on folders only

Post 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) }
:idea: Please read the FAQ and How to Request Help.
devster
Posts: 417
Joined: 06 Jun 2017, 22:56

Re: Map regex replace on folders only

Post 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.
I only work in black and sometimes very, very dark grey. (Batman)
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Map regex replace on folders only

Post 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
:idea: Please read the FAQ and How to Request Help.
Post Reply