Script: Shift + Renumber

Advanced Renamer forum
#1 : 06/01-23 09:46
Jan
Jan
Posts: 3
Hello,

I'm trying to set up a renaming script, unfortunately without sucess.

Problem:
I have about 3500 files, named from 0.png to 3499.png. I would like to rename them so that the last 210 pngs (3389 to 3499) are called now 0.png to 210.png and the rest is starting at 211 up to 3499 again.

So:
0 --> 211
1 --> 212
...
3389 --> 0
3390 -->1
...
3498 -->209
3499 --> 210

It should be easily be done with an if condition.

if x < 3500 - 210 then rename to 0, 1, 2
if x >= 3500 - 210 then 211, 212 etc

Unfortunately, I dont get the synthax wiht JavaScript to work. Can you help me?

Thank you very much!
BR Jan


06/01-23 09:46
#2 : 06/01-23 11:36
David Lee
David Lee
Posts: 1071
Your arithmetic is poor - the last 210 numbers should be 3289 to 3499!

All you need is:

x = (x + 211) % 3500;

"%" is the modulo operator: "A % B" will return the remainder when A is divided by B


06/01-23 11:36 - edited 07/01-23 00:40
#3 : 08/01-23 12:30
Jan
Jan
Posts: 3
Reply to #2:
Hi David,

thanks for the reply!

This is my code

function (index, item){
var val = (val + 211) % 3500;
return item.newName + '_' + val;

which I got finally without getting an error.

It produces:
001.png_NaN.png
002.png_NaN.png

while I was hoping for 001.png or 3499.png
It also seems not to change the the numbers at all.
What do I miss here?

Thanks again, BR Jan


08/01-23 12:30
#4 : 09/01-23 20:26
David Lee
David Lee
Posts: 1071
Reply to #3:

var val = (val + 211) % 3500;

Here you have declared a variable "val" implicitly within a formula but have not assigned it a value. Thus "val" will have type "undefined" within the expression.

Since "val" is not a numeric data type, the expression "(val + 211) % 3500" will then return the value NaN - ie "Not a Number".

You need to assign the numeric value of the current filename to val:
val = item.name*1

note that item.name will return a string value and must be converted to a number before performing any numeric operations - one way of doing this is to multiply the value by the number "1", as above.

More explicitly you could also write:
val = parseInt(item.name);

Be aware that...

item.name will return the ORIGINAL filename WITHOUT extension
item.newName will return the latest NEW filename INCLUDING extension
item.newBasename will return the latest NEW filename WITHOUT extension

This simple script will return the new names you specified in your reply #2...

return (item.name + '_' + (item.name*1 + 211) % 3500);

However, if you simply want to rename the files with the new name only this will lead to errors.
"return (item.name*1 + 211) % 3500;" will throw the error message "Possibility of multiple files with the same name".

This is because 'Renamer renames files in the list one by one and would fail if it tried to rename a file with the name of another file already existing in the same folder - even if the end result would not duplicate any filenames

To get around this you need to add something to each file to make sure that you do not try to create duplicate filenames eg...
return '#' + (item.name *1 + 211) % 3500);

Then reload all your files into the list and delete the temporary character in a separate batch.

If you want to zero-pad the result (say to n digits) then a simple trick is to add a string of n-1 zeros to the beginning of the result and then slice off and keep only the last n digits.
eg:
newName = (name + 211) % 3500;
return ('00' + newName).slice(-3);


09/01-23 20:26 - edited 09/01-23 20:31
#5 : 11/01-23 17:37
Jan
Jan
Posts: 3
Reply to #4:

Thank you so much David for your comprehensive answer! Very detailed, all questions answered. Thanks again!


11/01-23 17:37