Need help understand if/else,

All about user-defined episode / movie / file name format expressions
Post Reply
Crankrune
Posts: 21
Joined: 20 Jul 2016, 11:35

Need help understand if/else,

Post by Crankrune »

I've been using FileBot for while, using Google and these forums to figure out how to do what I've wanted to do. And so far that's worked most of the time. Except with if/else statements. I have a basic understanding of them, but haven't really figured out how they work. The only thing I've really figured out with them is this basic way of adding the video quality if it's 720p or 1080p.

Code: Select all

{if (vf=="720p") " [720p]" else ""}
But I have not idea how to use it when something is a variable. For example I'm trying to use it (if/else) to write "Special" if something is a special, and "Episode" if it's not. However the "{special}" outputs the special number, which would be different for each special, meaning without writing a thousand lines, doesn't work.

Similarly I've seen what looks to be another kind of if/else statement written with "?" & ":", but even less sure of how those work. One last thing, is it possible to have the output of the if/else be a variable e.g. "{s}" or "{e}"?

Please and thanks for any help.
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Need help understand if/else,

Post by rednoah »

The regular binding will give you a boolean value:

Code: Select all

{regular ? '' : ' [Special]'}
Since special will unwind-on-undefined, this will work as well:

Code: Select all

{special; ' [Special]'}
The outer {...} define an entire expression, not just a variable:

Code: Select all

{regular ? 'E'+e : 'SP'+special}
@see viewtopic.php?f=5&t=1895
:idea: Please read the FAQ and How to Request Help.
Crankrune
Posts: 21
Joined: 20 Jul 2016, 11:35

Re: Need help understand if/else,

Post by Crankrune »

Thanks for the help. Here's my whole expression.

Code: Select all

{file.path[0]}:\TV\{csv('H:/TV/Names.csv').get(n.replaceTrailingBrackets(replacement = "").replace(':', " -")) ?: n.replaceTrailingBrackets(replacement = "").replace(':', " -")}\{episode.special ? 'Specials/' : ''}{'Season '+s}{' '+[vf.match(/720[pP]|1080[pP]/)]}\{episode.special ? 'Special '+special.pad(2) : 'Episode '+e.pad(2)} - {t.replacePart(replacement = ' [Part $1]').replace("`","'").replace("?", "!").replace('"', "''").replace(':', " -")}{' '+[vc.match(/HEVC|x265/)]}
I do have a question about this, the part that adds the quality. I copied it from someone else in the 'format expressions' thread, and am not entirely sure how a couple parts of it work.

1. The forward slashes instead of quotations, why? Does it work differently or are they interchangeable?

2. The 'p' in brackets, what does that do?

Again, please and thanks for the help! FileBot is amazing!
User avatar
rednoah
The Source
Posts: 23924
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Need help understand if/else,

Post by rednoah »

1.
It's the same. /.../ is usually used for regular expressions because escaping ', " and \ is annoying.

There is many ways to define a String value:
http://groovy-lang.org/syntax.html#all-strings


2.
[pP] is a regular expression:
http://www.regular-expressions.info/quickstart.html

It means "p or P" but in this case it doesn't do anything because String.match(pattern) is case-insensitive by default.
:idea: Please read the FAQ and How to Request Help.
charlesellzy
Posts: 1
Joined: 23 Jul 2016, 22:41

Re: Need help understand if/else,

Post by charlesellzy »

I have learnt if-else statement in c, cpp, and java. This is a new to me indeed.
Post Reply