Scripting help: Change numbers from 01 to 01-02 For Each file in the directory

Advanced Renamer forum
#1 : 11/02-20 22:00
DevXen
DevXen
Posts: 4
Hello, I have files in a folder i need changed:
from 01 to 01-02

like so:

01 to 01-02
02 to 03-04
03 to 05-06
04 to 07-08
05 to 09-10
etc

i didn't see any preset that would do it. though i was able to change the single padded number to every other number. 01, 03, 05, 07, 09. then i was able to manually go in and add the rest. but i'm sure there is a better way to do it with Advanced Renamer.

I'm guessing in the script. but that seems just a bit too advanced for me.
have any suggestions?

Also the numbers are only part of the filenames. they generally have characters before and after the numbers.


thanks,
-Dev


11/02-20 22:00 - edited 11/02-20 22:46
#2 : 12/02-20 10:09
David Lee
David Lee
Posts: 517
Try this script:

name = item.name.match(/(.*)(\d{2})(.*)/);
n = name[2];
num1 = ("0" + (2*n-1)).slice(-2);
num2 = ("0" + 2*n).slice(-2);
return name[1] + num1 + "-" + num2 + name[3];


12/02-20 10:09
#3 : 13/02-20 05:14
DevXen
DevXen
Posts: 4
Reply to #2:

Thank you. that kinda works.

I was excited to see a reply to try when i got home.
and i struggled with and couldn't get it to work
until i renamed the file 01.

I might not of been clear. my filenames aren't 01, 02, etc
they just have those in them.
usually with a S01E01 or S02E02, or whatever ones i'm working on at the time.
usually. but sometimes it can vary. oh and maybe put an E before the second number

01 to S01E01-E02
02 to S01E02-E03

Since thats mostly what this script will be used for.

i was also thinking. sometimes it can vary. is there also maybe a way to add how many it adds like..

01 to 01-03
02 to 04-06

or

01 to 01-04
02 to 05-08

by hopefully just changing a number in the script to match the number for it to rename based on.

Thanks again.



13/02-20 05:14
#4 : 13/02-20 18:21
David Lee
David Lee
Posts: 517
Reply to #3:
My original script should work on the last two numerical digits in each filename.

so...
S01E01 -> S01E01-02
S02E02 -> S02E03-04

To change the range of added numbers you only need to replace "2" in the functions 2*n-1 & 2*n

I've modified the script below - change the value of "m" to be equal to the range required.
I've also modified the Regular Expression to extract the letter before the last number as a prefix to the new numbers, as in your example.

The script will also accept characters after the numbers - as long as they don't include a pair of numerical digits.

So with m = 2:
S02E02qwerty -> S02E03-04qwerty

and with m=3:
S02E02qwerty -> S02E03-04qwerty

m = 2;
name = item.name.match(/(.*)(\w{1})(\d{2})(.*)/);
app.log(name);
prefix = name[2];
app.log(prefix);
n = name[3];
num1 = prefix + ("0" + (m*(n-1)+1)).slice(-2);
num2 = prefix + ("0" + m*n).slice(-2);
return name[1] + num1 + "-" + num2 + name[4];



13/02-20 18:21 - edited 13/02-20 18:27
#5 : 14/02-20 05:47
DevXen
DevXen
Posts: 4
Reply to #4:

Thank you again for the reply. I tried this new one and it sounds like it should be perfect.

When I try it all the files turn red.

I thought maybe the filenames had other numbers in them and that was the issue, so i used the replace a few times to remove them. (First ones i tried had 2.0 and 480p in them. but after setting up the replaces, they were still red. I found other filenames without any other numbers. and tried 01, and S01E01 and the file is still red.


I tried to rename it anyways to see what it would do. and it said: error (124) Invalid Script

Here are a few of the filenames i was trying it with:

The.Sylvester.and.Tweety.Mysteries.S01E01.The.Cat.Who.Knew.Too.Much.480p.DVDRip.DD2.0.x264-SA77-78.mkv
The.Sylvester.and.Tweety.Mysteries.S01E02.Platinum.Wheel.of.Fortune.480p.DVDRip.DD2.0.x264-SA77-78.mkv
The.Sylvester.and.Tweety.Mysteries.S01E03.Double.Take.480p.DVDRip.DD2.0.x264-SA77-78.mkv

Then i tried:
The.Sylvester.and.Tweety.Mysteries.S01E01.The.Cat.Who.Knew.Too.Much.DVDRip..mkv
The.Sylvester.and.Tweety.Mysteries.S01E02.Platinum.Wheel.of.Fortune.DVDRip.mkv
The.Sylvester.and.Tweety.Mysteries.S01E03.Double.Take.DVDRip..mkv

Then i tried:
Sylvester and Tweety Mysteries 401 The Stilted Perch - A Game of Cat and Monster [videk].mkv
Sylvester and Tweety Mysteries 402 You're Thor! - I Gopher You [videk].mkv


Then i tried:
01. The Brain Machine-Joy Ride-Invasion of the Earthors-The Whirlpool.mkv
02. The Secret Four-Tiger on the Loose-The Mysterious Time Creatures-The Anditode.mkv
03. Invasion of the Hydronoids-Hitchhike-City in a Bottle-Space Emergency.mkv

Then i tried:
S01E01. The Brain Machine-Joy Ride-Invasion of the Earthors-The Whirlpool.mkv
S01E02. The Secret Four-Tiger on the Loose-The Mysterious Time Creatures-The Anditode.mkv
S01E03. Invasion of the Hydronoids-Hitchhike-City in a Bottle-Space Emergency.mkv

Then i tried:
Test S01E01. The Brain Machine-Joy Ride-Invasion of the Earthors-The Whirlpool.mkv


and they all had the same error.

oh I also tried to add:
return item.newBasename; to the end to see if that was the issue, since when you create a new script it adds that. and that didn't help either.


14/02-20 05:47
#6 : 14/02-20 11:21
David Lee
David Lee
Posts: 517
Reply to #5:

Are you sure that you entered the script accurately? It works for me when I copy and paste it from my Forum post.

The previous version will show all entries in the list as red if the match fails in any of the filenames, even though filenames that match are correctly renamed. I've corrected that problem in the version below by jumping out of the script if the match returns a null result.

In this version you can set the prefix by changing the value in the statement: prefix = 'E';
If you do not require to extract a prefix then set this to null - ie prefix = '';

The "app.log()" lines in the previous version were debugging commands and I have removed them.

m = 2;
prefix = 'E';
regex = '(.*)' + prefix + '(\\d{2})(.*)';
name = item.name.match(regex);
if (name == null) {return};
n = name[2];
num1 = prefix + ("0" + (m*(n-1)+1)).slice(-2);
num2 = prefix + ("0" + m*n).slice(-2);
return name[1] + num1 + "-" + num2 + name[3];


14/02-20 11:21
#7 : 14/02-20 20:13
DevXen
DevXen
Posts: 4
Reply to #6:

So I tried it and it still didn't work.
then i got thinking. i wonder if i was on an older version of the program. turns out iu was on 3.83.
I updated it and its working beautifully. thank you so much for your help. thats going to save me so much time. you don't even know.

-Dev


14/02-20 20:13
#8 : 15/02-20 11:21
David Lee
David Lee
Posts: 517
Reply to #7:

That's strange - my latest script runs perfectly in ver 3.83 for me.

The previous version would have shown errors since Kim only introduced JavaScript logging in ver 3.84 so "app.log()" would not have been recognized in earlier versions.


15/02-20 11:21