Pre batch - get smallest imagewidth of all items in list

Advanced Renamer forum
#1 : 08/02-20 20:23
Posts: 5

I need a script that does the following:

Set the startnumber for files.
Rename items starting with the startnumber. ("Name"+(startnumber+index);)
If the file is ~2x the size of a regular image, then name it ("Name"+(startnumber+index)+"-"+(startnumber+(index+1));).

So for example:
Picture 1: 300 width
Picture 2: 600 width
Picture 3: 300 width

Startnumber = 5
"Picture 5"
"Picture 6-7"
"Picture 8"

Pre-batch: Iterate through all images in the list and get the item.imgWidth.


var minSize=9999;

if( item.imgWidth < minSize ) {
minSize = item.imgWidth;


Main script:
function(index, item){
var val = index;
if (item.imgWidth > minSize*1.6){
return "Name "+index+"-"+(index+1);
} else {
return "Name "+index;


The problem is that "index" is the wrong variable in this case. It had to be a "startNumber" variable that I can set before I start the batch.

For example, I have pictures 1-10 in one folder and 11-20 in another. I'd want to apply the script to folder one with the startNumber=1 and then folder two with startNumber=2.

But I don't know how to do that. Any ideas?

08/02-20 20:23
#2 : 09/02-20 20:11
David Lee
David Lee
Posts: 517
Pre batch script code to determine the smallest image width:

n = app.itemCount;
minWidth = app.getItem(0).imgWidth;
while (j < n) {
minWidth = Math.min(minWidth, app.getItem(j).imgWidth);

I don't quite understand your problem in the final paragraph of your post.

09/02-20 20:11 - edited 09/02-20 23:10
#3 : 16/02-20 20:49
Posts: 5
Reply to #2:
Thank you very much! That worked fine, kinda.

To test it I added
number = 0;
in the pre batch script. And then tried for the main script:

if (app.getItem(index).imgWidth>minWidth*1.5){
return "name "+(number-1)+"-"+(number);
} else {
return "name "+(number);

I think it worked a few days ago, then I tried to look up how I could get it to add up to two leading zeros (1 -> 001; 10 -> 010), but coming back it starts throwing errors:

"Access violation at address 6328F7B5 in module 'js32.dll'. Read of address 02C2F77C."

When it throws this error, the file seem to be in the list, but they are "invisible".
I have a folder with 7 files in it. When I have AutoTest enabled and add more than 3 files at once, it throws the error.
When I have AutoTest disabled, it works fine. Even when I throw in all 7 files at once.
But when I try to TestBatch, it throws the same error, but the files don't disappear.

Edit: I found that there is a <Inc Nr:X:Y> tag that increases the a filenumber and adds leading zeros. But I am not sure how to use that inside code yet.

It looks like I can't simply slap that onto a string?
return "name "+<Inc Nr:1:1>;


Regarding the final paragraph:
I was looking for either:
a) a way to tell the script with what number it should start, since I have files in several folders that should start with different numbers.

or b) if I give the first file a number, it should read that number and use that as a startpoint.
There are files named "IMG0001" to "IMG0010" in a folder "rename" that I want to rename.
I have a folder with pictures called "Garden_001" to "Garden_025".

Then I'd like to sort the images in the folder "rename" by creation date, rename the first image in that folder to "26" and then it would be nice if the program could read that number and set it as a startpoint. Then it would rename the files into "Garden_026", "Garden_027" and so on.

But thank you for your help so far!

16/02-20 20:49 - edited 18/02-20 13:01