Replace existing numbers with matching to new ones?

A swapping-ground for Regular Expression syntax

Replace existing numbers with matching to new ones?

Postby Breaky » Fri Apr 23, 2021 2:10 pm

Hi,

I had: Multiple files named "S01E001 - title of episode" to "S01E483 - title of episode". All those episodes are part of 9 seasons.
So I placed all relevant episodes in corresponding season folders and changed out "S01" to "S0x", where "x" is the correct season number.

However, season 9 for example, the episode numbering should go from S09E001 to S09E039.

S09E383 corresponds with S09E001
S09E421 corresponds with S09E039

So I want to somehow match and replace this AND skip the missing numbers. So for example episode 385 is missing (so no 003), but with 386 it keeps counting and thus chaning that one to 004.

I hope I'm explaining this right. Would love some help with this, any help is really appreciated!

Regards,

Breaky
Breaky
 
Posts: 3
Joined: Fri Apr 23, 2021 1:23 pm

Re: Replace existing numbers with matching to new ones?

Postby Luuk » Fri Apr 23, 2021 6:10 pm

The RegEx(1) will not conduct math, so it could never conduct the match/replace-with-numbering.
Im thinking only javascript will conduct match/replace-with-numbers (when numbers require the math).
I can describe the math, if there is experts to convert this math into the javascript?...

First, look at the first name (lowest E###).. Subtract 1 from ### to set your variable.
So if the very first filename was S09E383... Set a variable to 382... Then just subtract 382 from the E### numbers...
E383 ==> E001 (383-382)
E384 ==> E002 (384-382)
E385 ==> E003 (385-382)
E390 ==> E008 (390-382)
E391 ==> E009 (391-382)

Its unfortunate, but I dont know how to conduct the javascript, except like inside of the Filters(12) condition.
If its a big problem, I could write a batch for this, but hoping there is experts to explain this in the javascript.
Maybe another way, if not too many missing episodes, is to create the counterfeit episodes, and then using Numbering(10).
Luuk
 
Posts: 323
Joined: Fri Feb 21, 2020 10:58 pm

Re: Replace existing numbers with matching to new ones?

Postby Breaky » Mon Apr 26, 2021 11:01 pm

Hi Luuk, thanks for your reply.

Your solution sounds so simple. Just subtract the number of the first episode (-1) from each episode number in the filename. I've tried googling for a solution (in Windows) but am unable to find a solution (I understand and can work with haha).

I found this:https://stackoverflow.com/questions/55888298/rename-the-end-of-filename-by-subtracting-value but I'm not sure how to use this and what the posts are referring to.
Breaky
 
Posts: 3
Joined: Fri Apr 23, 2021 1:23 pm

Re: Replace existing numbers with matching to new ones?

Postby Admin » Tue Apr 27, 2021 3:18 am

Hi, what would be the math to be applied to each name? Then once the math is known, it can be done with Javascript in Bulk Rename Utility?
Admin
Site Admin
 
Posts: 2059
Joined: Tue Mar 08, 2005 8:39 pm

Re: Replace existing numbers with matching to new ones?

Postby Luuk » Wed Apr 28, 2021 4:24 am

Javascript does easily conduct the math, because look at this... viewtopic.php?f=2&t=4849&p=13030 except that its conducting math to the end of filenames, but the number for conducting math does not come from the lowest filename in each folder.

So it still needs a way to conduct math in the middle, but also setting a variable for each new folder, OR just set a variable manually to conduct one folder at a time. Its easy conducting this variable with a batch, but Im no idea how the javascript would conduct different "math per folder", unless maybe someone renamed each parent folder to have the number needed for the math.

If its me, Im thinking the easiest way is to invent a batch using something command-line like the exiftool, but I would need to know many things like the file extensions, if there is other files to never conduct, etc. But after knowing the environment, Im thinking it would be very easy to invent a batch for this.
Luuk
 
Posts: 323
Joined: Fri Feb 21, 2020 10:58 pm

Re: Replace existing numbers with matching to new ones?

Postby Breaky » Fri May 21, 2021 4:41 pm

Luuk wrote:Javascript does easily conduct the math, because look at this... viewtopic.php?f=2&t=4849&p=13030 except that its conducting math to the end of filenames, but the number for conducting math does not come from the lowest filename in each folder.

So it still needs a way to conduct math in the middle, but also setting a variable for each new folder, OR just set a variable manually to conduct one folder at a time. Its easy conducting this variable with a batch, but Im no idea how the javascript would conduct different "math per folder", unless maybe someone renamed each parent folder to have the number needed for the math.

If its me, Im thinking the easiest way is to invent a batch using something command-line like the exiftool, but I would need to know many things like the file extensions, if there is other files to never conduct, etc. But after knowing the environment, Im thinking it would be very easy to invent a batch for this.


I have folders going from "Season 1" to "Season 9"
Filenames look like: S01EXXX - title.extension
The extensions are .mkv and .mp4
There are no other files in the folders at this point

Hope this helps.


I tried finding a solution, but Javascript etc. is something I really don't understand. So for now I'm guessing I should just try to get the series complete so I can rename them using the numbering option (and deleting the existing numbers).
Breaky
 
Posts: 3
Joined: Fri Apr 23, 2021 1:23 pm

Re: Replace existing numbers with matching to new ones?

Postby Luuk » Fri May 21, 2021 11:47 pm

I can post a batch to conduct the math, but please to consider the troubles for missing episodes like...

Series 1/S01E001-Title.mpg ---> Series 1/S01E010-Title.mpg (any E### can be missing)
Series 2/S02E011-Title.mpg ---> Series 2/S02E020-Title.mpg (any E### can be missing)
Series 3/S03E024-Title.mpg ---> Series 3/S03E030-Title.mpg (any E### can be missing)

So if missing any first episodes in a series, the math incorrectly renames S03E024 ==> S03E001 !!
You can add logic for missing #s between series, but how to know which series E022 should belong ??

Another problem can be, if missing S02E015-S02E018, then 014-->004 and 019-->009, and this is all good.
But then later, when you get the download S02E017, how to know where 017 belongs inbetween 004-009 ??
So Im thinking this experiment NOT to be a good idea, unless missing very few episodes, and never the first ones in a series.

But this batch will conduct the different math per folder, like your description...

@echo off
cd /d "C:\Your\Main\Folder"
SetLocal EnableDelayedExpansion
For /f "delims=" %%A IN ('dir /b /s /ad "Season ?"') DO (
For /F "tokens=2 delims=E " %%B IN ('dir/b "%%A\S??E??? - *" 2^>nul ^|Findstr /n . ^|Findstr ^1:') DO (
Set/a num="(1000%%B %% 1000)-1"
Exiftool -q -fast4 -ext mkv -ext mp4 -TESTname"<${Filename;s/(S.*E)(\d{3})(.*)/$1$2$3/;$_=$1.sprintf('%%03d',$2-!num!).$3}" "%%A" 2>nul
echo.))
pause

Since its using -TESTName instead of -FileName, it only presents a preview for how the new names would look, so looking like...

..../Season 3/S03E077 - Title.txt --> ..../Season 3/S003E001 - Title.txt
..../Season 3/S03E078 - Title.txt --> ..../Season 3/S003E002 - Title.txt
..../Season 3/S03E079 - Title.txt --> ..../Season 3/S003E003 - Title.txt

..../Season 4/S04E080 - Title.txt --> ..../Season 4/S004E001 - Title.txt
..../Season 4/S04E081 - Title.txt --> ..../Season 4/S004E002 - Title.txt
..../Season 4/S04E199 - Title.txt --> ..../Season 4/S004E156 - Title.txt

..../Season 5/S05E001 - Title.txt (no change if first-file is already 001)
..../Season 5/S05E045 - Title.txt
..../Season 5/S05E999 - Title.txt

..../Season 11/S11E427 - Title.txt (no change if folder has two digits, so could change "Season ?" ==> "Season *")
..../Season 11/S11E045 - Title.txt
..../Season 11/S11E999 - Title.txt

Except that ..../ can be very long paths, so oftem Im just add >Results.txt to the end of my exiftool line.
So then I have a results file, where its easy to replace the long paths into something much shorter for the eyesight.
Changing -TESTName ==> -FileName will rename the files.. So please dont use this, if you are missing too many episodes!
Luuk
 
Posts: 323
Joined: Fri Feb 21, 2020 10:58 pm


Return to Regular Expressions