Naming formats - using a function on the entire file name

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
ImaginaryTango
Posts: 9
Joined: 16 Sep 2023, 22:15

Naming formats - using a function on the entire file name

Post by ImaginaryTango »

This is the kind of feature that seems so simple to find, I'm probably using the wrong terms to look it up.

I'm using the .validateFileName() function and it still leaves commas and apostrophes in the string, so I'm using an additional function for each of those characters. It turns out I have to do this for the TV show name and for the episode title. Is there any way to easily apply functions to the entire filename when renaming? I tried grouping with a set of brackets, braces, and parenthesis, and those did not work.
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Naming formats - using a function on the entire file name

Post by rednoah »

Yes, but you would have to write your entire expression inside a single {...} returning one single normalized file path.


:arrow: @files › Script Parameter Value happens to have a prime example for this kind of format expression:

Format: Select all

{
	[ allOf {ny}
	        { allOf {vf}
	                {group}
	        }
	, allOf {ny}
	        {'CD'+pi}
	        {lang}
	]*.findResults { 
	    if (it) it.toString()
	              .replaceAll(/[!?.]+$/)
	              .asciiQuotes()
	}*.join(' ')
	  .join('/')
}


ImaginaryTango wrote: 25 Sep 2023, 06:33 I'm using the .validateFileName() function and it still leaves commas and apostrophes in the string
Note that Windows does allow commas and apostrophes in file names, but I can see why you might have your own reasons why you might not want certain characters in your file paths.
:idea: Please read the FAQ and How to Request Help.
ImaginaryTango
Posts: 9
Joined: 16 Sep 2023, 22:15

Re: Naming formats - using a function on the entire file name

Post by ImaginaryTango »

I'm trying to use what I see there - and writing it all in a single {...} grouping, but that doesn't seem to work at all.

It's not so much about what's allowed, but what works for me. My media server is on Linux, but my other devices are on various OSes. Some characters are just a pain to have in a file name so I avoid them.
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Naming formats - using a function on the entire file name

Post by rednoah »

ImaginaryTango wrote: 26 Sep 2023, 15:15 I'm trying to use what I see there - and writing it all in a single {...} grouping, but that doesn't seem to work at all.
:?: Perhaps you could paste what you have so far here, so we can see what exactly doesn't seem to work at all?
:idea: Please read the FAQ and How to Request Help.
ImaginaryTango
Posts: 9
Joined: 16 Sep 2023, 22:15

Re: Naming formats - using a function on the entire file name

Post by ImaginaryTango »

Here's what I'm using now:

Format: Select all

{n.validateFileName().upperInitial().space('_').removeAll(/[()"',]/).replaceAll('&', 'And')}-{y}-S{s.pad(2)}E{e.pad(2)}-{t.validateFileName().upperInitial().space('_').removeAll(/[()"',]/).replaceAll('&', 'And')}
Whenever I try to enclose the entire string in any kind of grouping, parenthesis, bracket, or brace, it doesn't work.

I'm trying to do a format like:

Code: Select all

Program_Name-YYYY-SxxExx-Episode_Title
I'm also running into an issue for old cartoons, like The Herculoids, that have 2 stories per episode, but the online DBs count each story as an episode, so I have multiple episodes per file, like:

Code: Select all

The_Herculoids-1967-S01E01-02-Episode_Title
WIth that format, I'm trying to figure out how to fit in the extra "-xx" for the 2nd (or 3rd) episode so it's there when needed and not there if it's a normal show, with only one episode per show.
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Naming formats - using a function on the entire file name

Post by rednoah »

1.
Looks like you want to just use the {plex} format format?

Format: Select all

{ emby }

Format: Select all

{ emby.name }
:?: Are you very set on not using spaces and replacing ampersands? Maybe like this?

Format: Select all

{ emby.name.upperInitial().space('_').removeAll(/[()"',]/).replaceAll('&', 'And') }

Code: Select all

Firefly_2002_-_S01E01_-_Serenity


2.
You'll want to use the built-in {s00e00} binding, or just the {plex} format. Those will take care of multi-episode formatting. (NOTE: assuming multi-episode detection is working in the first place for the files at hand; we can't check because you didn't post input file paths as text)
:idea: Please read the FAQ and How to Request Help.
ImaginaryTango
Posts: 9
Joined: 16 Sep 2023, 22:15

Re: Naming formats - using a function on the entire file name

Post by ImaginaryTango »

I think you've solved all my problems. There's a few changes I'd make, but I think I can see how to make them now. (Like using just '-' instead of '_-_' because it's easier to read.) I tend to try to avoid spaces, ampersands and other irregular characters because I'm a heavy command line user and like to avoid characters I have to escape there.

I went through a cheat sheet that was quite helpful, but I guess I missed some points. I may have seen "emby" and skipped whatever it said, since I've tried Emby. It's not bad and I was between Emby and Plex, but am going with Plex, although I don't like some things about their client on Apple TV. (And I'm NOT going to use Infuse - they have one bug they've been stubborn about and rather than finding a way to do the same thing in a better way, they're doubling-down on it and ignoring Apple's directives and it's created a serious pain for me.)

When you have multiple functions attached to a function, like this example you gave that you used for Firefly (I'm not clear how to copy it and paste it with formatting on this forum software!), are the functions applied in order? For instance, is the ".space('_')" function is applied before the two that follow it? I was thinking I can add a ".replaceAll('_-_', '-')" at the end of the list of functions on the end. That way it does that at the end of the process, after spaces are replaced with underscores.
ImaginaryTango
Posts: 9
Joined: 16 Sep 2023, 22:15

Re: Naming formats - using a function on the entire file name

Post by ImaginaryTango »

Okay, testing and I think this:

{plex.name.upperInitial().space('_').removeAll(/[()"',]/).replaceAll('&', 'And').replaceAll('_-_', '-')}

is pretty close to working for me - I may have to shift to Emby, if that include the year in it. I've been having a lot of trouble with shows getting the right info and matches without the year included.
ImaginaryTango
Posts: 9
Joined: 16 Sep 2023, 22:15

Re: Naming formats - using a function on the entire file name

Post by ImaginaryTango »

Emby works - except for one thing. While it's not critical, it'd help me in terms of readability. Currently, with Emby, I get:

Series_Name_YYYY-SxxExx-Episode_Title

That's using what I have in the post above, but with emby replacing plex. But one small thing that confuses me visually when I try to read it. Replacing the spaces gives me an underscore before the year and I'm wondering if there's a way to make it a dash. I know it's small, but visually it makes it MUCH easier for me to read. (I have reading issues...)

So would there be some way to replace "_YYYY" or " YYYY" with "-YYYY" at some point in the process?
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Naming formats - using a function on the entire file name

Post by rednoah »

You'll want to include the {tmdb-id} pattern if you're preparing file paths for Plex, especially if you make changes (i.e. all space / remove / replace operations) the the Series / Movie name. {ny} is better than {n} for sure and it helps, but it doesn't really solve the underlying problem. Only {tmdb-id} solves the underlying problem.


:arrow: See How do I organize files for Plex? for details.


:?: If you want Name-Year to suit your own preferences rather than Plex, then we can help you make it work the way you want. However, if you want Name-Year to help Plex identify the movie / series, then you should really use {tmdb-id} instead, and / or add *.nfo files with the TMDB ID at the very least.


:?: Do you want to replace all spaces / underscores / etc with - dash, or just for Name-Year specifically, and otherwise use _ for other spaces?


:?: You may also want to consider having a primary file structure with proper standard naming, and then mirror disposable secondary structures with different naming / organization for different use cases as needed. See Re-organize previously organized files using local xattr metadata for details.
:idea: Please read the FAQ and How to Request Help.
ImaginaryTango
Posts: 9
Joined: 16 Sep 2023, 22:15

Re: Naming formats - using a function on the entire file name

Post by ImaginaryTango »

The only thing I'd like to change in the naming is just to use a dash after the show title and before the year. I've been doing that all along. (Plex is okay with it - as long as I do the folder name like it wants.) I have reading issues, basically a symbol or pattern recognition situation, and I use a dash to separate the different elements of the file names. I put a dash between title and year, year and SxxExx, and between that episode number and the episode title. For me, with my reading issues, when I have a dash between the show title and year, I have to look twice to see if the year is part of the title or is the production year.

For shows, I use a folder like Plex specifies:

The_Good_Place (2016)

Although I've found, for readability and sorting within the file system, I can usually drop "The" or "A" from the front of a title. Sometimes Plex doesn't match a show when I do that, but it's easy to fix that within Plex and pick the match for it. Then, for the files, I do not use the season folders. (I tested that first before dropping that.) I do use a format like this:

The_Good_Place-2016-S01E01

Filebot matches that easily enough and adds the episode title for me. And you're the first person that's mentioned what I do with the file structure. I have a network share, "Library," that has all my media - music, dramatic recordings, old time radio shows, and videos, including movies, TV movies, and TV shows. I really prefer a hierarchical organization, which makes it MUCH easier for me to find something in the file archive. For instance, I have Video, then, in that directory, Movies, TVMovies (which includes some movies that would be a special in the series, like Return to Mayberry, but also movies that weren't part of the actual series, like the Red Green specials, and I dump in movies that were made-for-TV, to separate them from theatrical movies), and TVShows. Under TVShows, I have categories like Comedy, Drama, SFAndF (Science Fiction and Fantasy) and sometimes subcategories, like, under SFAndF, I have folders for Stargate, Doctor Who, and Star Trek.

I find it frustrating that Plex doesn't like that kind of organization and can't really deal with subdirectories. Since my server is a Synology RAID and runs on their locked down Linux, it makes symlinks easy to use. So, in Library, I have a folder, Plex. And under Plex, I have a simpler organizational scheme. While I have my subfolders under TVShows (Comedy, Drama, etc...), I specified each of those categories as a folder for one Plex library. (But I don't like that I can't put groups under other groups - I can't have a group, "Star Trek," for example, under the group SFAndF. So I have all my symlinks under Plex, in its own place. I was testing out Emby as well - and it's still possible I could move to Emby. There are things I like in both, but it's easier to deal with the things I don't like in Plex than the things I don't like in Emby. With symlinks, space use for multiple forms of organization are minimal and I can have a directory tree for Plex and one for Emby, which I've done while still looking into both.

With the show I was testing for multiple episodes, I have:

The_Herculoids-1967-S01E01-E02-Episode_titles

Note I changed the underscore before the year to a dash. With that in a folder named "Herculoids (1967)" Plex has no trouble recognizing it. (But then there's another pesky issue - that Plex lists the file twice, once under each sub-episode number/title - but that's a known issue with Plex.)

Oh - just realized - I have another level of abstraction involved, other than just a separate directory tree for Plex and one for Emby. Plex runs on one server that I use for several media functions (like as a DVR). All my files are on the SSD RAID and they are mounted as a Samba share on the main server.
User avatar
rednoah
The Source
Posts: 22998
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Naming formats - using a function on the entire file name

Post by rednoah »

I see. So to conclude this thread here's a format that you can use, putting the file name together in a single expression, so that you can call all your transform functions on the entire file name:

Format: Select all

{ allOf{n}{y}{s00e00}{t}.join('-').upperInitial().space('_').replace('&':'And').removeAll(/[()"',]/) }

Code: Select all

The_Good_Place-2016-S01E01-Everything_Is_Fine



:idea: This structure if for your personal preferences. If you're using Plex, then I strongly recommend also having a separate (auto-generated based on xattr metadata from your primary structure) where the same files are organized correctly with {plex.id} as format. If you use hardlinks then there's no downside to having the same files twice or thrice at different file paths. But that's a topic for another thread.
:idea: Please read the FAQ and How to Request Help.
Post Reply