Rename sporting event

Any questions? Need some help?
Post Reply
martinc
Posts: 13
Joined: 03 Jan 2019, 17:46

Rename sporting event

Post by martinc »

Hi,
I'm looking to strip a lot of extra content out of filenames and sort in a logical manner.

My files are named something like this:
EPL.2018.12.30.Manchester.United.vs.Bournemouth.720p.WEB.h264-VERUM.mkv

I'm using the following to get them sorting without matching to a series etc:
"C:\Program Files\FileBot\filebot.exe" -script fn:amc "W:\EPLReplays" --output "W:\EPL" --action move -non-strict --log-file "C:\VOD\epl.log" --def seriesFormat="W:\EPL\{folder.name}\{folder.name}" --def unsorted=y "ut_label=%L" "ut_state=%S" "ut_title=%N" "ut_kind=%K" "ut_file=%F" "ut_dir=%D"

This get's me files like:
W:\EPL\EPL.2018.12.30.Manchester.United.vs.Bournemouth.720p.WEB.h264-VERUM\EPL.2018.12.30.Manchester.United.vs.Bournemouth.720p.WEB.h264-VERUM.mkv

That's OK, but there's a load of extra content in there I don't want. I'm looking to get it to something like:
W:\EPL\2018.12.30 - Manchester United vs Bournemouth\2018.12.30 - Manchester United vs Bournemouth.mkv

I'm sure I need to do some regex work and tried but failed. Is there anyone that can point me in the right direction please and get me started at least?

Many thanks!
User avatar
rednoah
The Source
Posts: 22899
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Rename sporting event

Post by rednoah »

Yes, you will need a lot of regex to match all the patterns that your files come in.


1.
Come up with a regex that matches and captures the different parts of the filename:
https://regexr.com/

e.g.

Code: Select all

(\w+).(\d{4}.\d{2}.\d{2}).(.+?).\d{3,4}p

2.
Write Groovy code to match the current file name and generate new file paths accordingly:

Code: Select all

{
	def m = (fn =~ /^(\w+).(\d{4}.\d{2}.\d{2}).(.+?).\d{3,4}p.+$/)[0]
	def s = m[1]
	def d = m[2]
	def t = d + ' - ' + m[3].space(' ')
	
	s / t / t
}
:idea: Please read the FAQ and How to Request Help.
martinc
Posts: 13
Joined: 03 Jan 2019, 17:46

Re: Rename sporting event

Post by martinc »

This is really helpful, and I've pretty much worked out the regex:

Code: Select all

(\bEPL)(\b.)(\d+.\d+.\d+)(\b.)(\D+)
Groups 3 and 5 give me what I need (though I have an annoying trailing ".")!

I think I can follow how your Groovy code is constructed, but I'm not sure how I'd implement that in my filebot call?

Thanks again!
martinc
Posts: 13
Joined: 03 Jan 2019, 17:46

Re: Rename sporting event

Post by martinc »

To add, I can see that I can remove the trailing . using Groovy script:
https://www.leveluplunch.com/groovy/exa ... om-string/
User avatar
rednoah
The Source
Posts: 22899
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Rename sporting event

Post by rednoah »

This should work:

Code: Select all

$ filebot -rename *.mkv --db file --action test --format "{
def m = (fn =~ /^(\w+).(\d{4}.\d{2}.\d{2}).(.+?).\d{3,4}p.+$/)[0]
def s = m[1]
def d = m[2]
def t = d + ' - ' + m[3].space(' ')

s / t / t
}"
Rename files using [Plain File]
[TEST] from [EPL.2018.12.30.Manchester.United.vs.Bournemouth.720p.WEB.h264-VERUM.mkv] to [EPL/2018.12.30 - Manchester United vs Bournemouth/2018.12.30 - Manchester United vs Bournemouth.mkv]
Processed 1 files
Best to make your own PowerShell script to make things more pretty, and to make use of it from a simple single-line command.
:idea: Please read the FAQ and How to Request Help.
martinc
Posts: 13
Joined: 03 Jan 2019, 17:46

Re: Rename sporting event

Post by martinc »

Amazing - thank you!!
martinc
Posts: 13
Joined: 03 Jan 2019, 17:46

Re: Rename sporting event

Post by martinc »

Sorry to be back, but this complains about the * character when I try it.

Image
User avatar
rednoah
The Source
Posts: 22899
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Rename sporting event

Post by rednoah »

1.
You know what *.mkv on the command-line means, right? Are there any *.mkv files in your current working directory?
https://en.wikipedia.org/wiki/Glob_(programming)


2.
Since *.mkv doesn't expand to any actual file path, "*.mkv" is literally passed in as file argument, and this file of course doesn't match our regex pattern, so we fail to get the first pattern match (at index 0), meaning our format code fails (as it should, because input is invalid in the first place).
:idea: Please read the FAQ and How to Request Help.
martinc
Posts: 13
Joined: 03 Jan 2019, 17:46

Re: Rename sporting event

Post by martinc »

Thanks - yes - I'm familiar with *.mkv, and have one in the folder...
Image
User avatar
rednoah
The Source
Posts: 22899
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Rename sporting event

Post by rednoah »

1.
Yes, now that the MKV file is there, the first Illegal Argument warning went away.


2.
You only have one MKV file in this folder, and the name is fc382d35*.mkv so it doesn't match our regex at all, and so our format fails. What exactly are you trying to do? Rename the folder? Rename files in that EPL* folder? But ignore the fc382d35*.mkv file?
:idea: Please read the FAQ and How to Request Help.
martinc
Posts: 13
Joined: 03 Jan 2019, 17:46

Re: Rename sporting event

Post by martinc »

Sorry - my clarity hasn't been good!

This is my situation:
Image

1) I have many folders with names similar to what I want - i.e. they match the regex.
2) Within each there is a MKV with a random name
3) I want to move the folder to the EPL directory, rename the folder and rename the file as per the folder name.

Exactly as per the below but with the folder structure above.

Code: Select all

$ filebot -rename *.mkv --db file --action test --format "{
def m = (fn =~ /^(\w+).(\d{4}.\d{2}.\d{2}).(.+?).\d{3,4}p.+$/)[0]
def s = m[1]
def d = m[2]
def t = d + ' - ' + m[3].space(' ')

s / t / t
}"
Rename files using [Plain File]
[TEST] from [EPL.2018.12.30.Manchester.United.vs.Bournemouth.720p.WEB.h264-VERUM.mkv] to [EPL/2018.12.30 - Manchester United vs Bournemouth/2018.12.30 - Manchester United vs Bournemouth.mkv]
Processed 1 files
Again, apologies for my poor explanation, and thanks for your patience.
User avatar
rednoah
The Source
Posts: 22899
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Rename sporting event

Post by rednoah »

1.
I see, fn is the file name, so that's of course meaningless in your case. Just replace fn with file.path so we match against the full file path, and not just the file name. You will need to remove ^ and $ from your regex since you're not matching the file name from beginning to end, but just a subsection of the file path.


2.
You also have to specify -r on the command line to process folders recursively (and not just files in the current folder).

Select mkv files in the current folder:

Code: Select all

filebot -rename *.mkv
Select the current folder, and tell FileBot to process folders recursively:

Code: Select all

filebot -rename -r .

3.
Since your format expects a specific file path pattern, it might be prudent to also tell FileBot to only process files that match this pattern, like so:

Code: Select all

--file-filter "file.path =~ /(\w+).(\d{4}.\d{2}.\d{2}).(.+?).\d{3,4}p.+/"

4.
You may also want to specify --output to specify the absolute output folder for your relative output format:

Code: Select all

--output X:/Sports
:idea: Please read the FAQ and How to Request Help.
martinc
Posts: 13
Joined: 03 Jan 2019, 17:46

Re: Rename sporting event

Post by martinc »

Thanks again, though I am clearly stupid as cannot get this working.

This is what I have used, I appreciate this may be incorrect, though I have tried adding/removing various elements in desperation. I've tried removing the *.mkv including others and I either get "no match" (if removing the *.mkv) which makes sense, or an error relating to the * character.

Code: Select all

filebot -rename -r *.mkv --file-filter "file.path =~ /(\w+).(\d{4}.\d{2}.\d{2}).(.+?).\d{3,4}p.+/
"  --format "{
>> def m = (fn =~ /^(\w+).(\d{4}.\d{2}.\d{2}).(.+?).\d{3,4}p.+$/)[0]
>> def s = m[1]
>> def d = m[2]
>> def t = d + ' - ' + m[3].space(' ')
>>
>> s / t / t
>> }" --output W:/EPLtest
Even the most basic command seems to have an issue with passing *.mkv. I know this is incorrect, but shows the recursion is working and an * related error in each folder.

Image

Sorry - I'm probably being an idiot but I'm trying various things and working hard to understand how this works.

If it would help for me to upload example files and batch file let me know.

Apologies again,
Martin
User avatar
rednoah
The Source
Posts: 22899
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Rename sporting event

Post by rednoah »

1.
We're back to the original thing. :lol:

Let's review our shell skills~

Remember that *.mkv is not something that is specific to FileBot, but PowerShell. There is no mkv file in the current folder, so PowerShell will not expand the *glob and you're essentially asking FileBot to rename a file called "*.mkv" (and not all mkv files in this folder). Now if there was an mkv file, it still wouldn't work, because we want mkv files in the various sub folders of the current folder, so *.mkv doesn't give us those files either.

. has special meaning. It means "the current working directory", just like .. which means "the parent folder"

YES

Code: Select all

filebot -rename -r .
YES

Code: Select all

filebot -rename -r X:/TestFiles
NO

Code: Select all

filebot -rename -r *.mkv
NO

Code: Select all

filebot -rename *.mkv

2.
As mentioned before, we need to change value we want to check for pattern (i.e. full file path, not just file name) and slightly adjust the regex for that:

This should do:

Code: Select all

{
def m = (file.path =~ /(\w+).(\d{4}.\d{2}.\d{2}).(.+?).\d{3,4}p.+/)[0]
def s = m[1]
def d = m[2]
def t = d + ' - ' + m[3].space(' ')

s / t / t
}
:idea: If you could share me a drive folder with test files and test scripts, then maybe I could have a quick look. Though I usually prefer to teach so you can understand and come up with the solution yourself, rather than just giving it to your verbatim. It's mostly shell and regex, extremely useful in combination, for all kinds of things, once you play with it a little. ;)
:idea: Please read the FAQ and How to Request Help.
martinc
Posts: 13
Joined: 03 Jan 2019, 17:46

Re: Rename sporting event

Post by martinc »

Definitely keen to learn - let me see what I can achieve with this information :)
martinc
Posts: 13
Joined: 03 Jan 2019, 17:46

Re: Rename sporting event

Post by martinc »

Success!!!!
Image

That's wonderful. Thank you so - much. I've learnt a lot!!! I can't promise I won't have questions in the future, but there should be less!!!!!
User avatar
rednoah
The Source
Posts: 22899
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Rename sporting event

Post by rednoah »

Not that hard after all! :D
:idea: Please read the FAQ and How to Request Help.
martinc
Posts: 13
Joined: 03 Jan 2019, 17:46

Re: Rename sporting event

Post by martinc »

:D
Post Reply