File renaming to base 26

Hi everyone. A long time ago I got a great rename script with the help of experts on this forum. It renames photo files (both jpg and raw) with my initials RGC followed by date taken yymmdd followed by an alphabetic group of up to three characters AA to ZZ to AAA to ZZZ, incremented by one letter per file for that day. This last group is used to distinguish between photos taken on the same day. Yes I could have used the time but this is shorter.
It works very well but for a small annoyance. Once the date group turns over to the next day the alphabetic group does not reset to AA. I want to be able to recognise from the filename the early pictures in a day's shooting. Can anyone advise? It's way beyond me now. Script is below and thanks for your time.
Robert, Australia

function OnGetNewName(getNewNameData)
{
	var prefixStr = "RGC";
	var padLen = 2;

	var meta = getNewNameData.item.metadata.image;
	if (meta == null || meta.datetaken == null)
		return getNewNameData.item.name;
	var dateTakenDate = DOpus.Create.Date(meta.datetaken);
	var dateTakenStr = trimTo(dateTakenDate.year,2) + padTo(dateTakenDate.month,2,"0") + padTo(dateTakenDate.day,2,"0")

	var numStr = getNewNameData.newname;
	if (!/^(\d+)$/.test(numStr))
		return true; // Do not rename.
	var idNum = Number(numStr);

	var startStr = getNewNameData.custom.last_id;
	if (startStr)
	{
		var startNum = 0;
		var charCode;
		var anyDigits = false;
		startStr = startStr.toUpperCase();
		while(startStr.length > 0)
		{
			charCode = startStr.charCodeAt(0) - 65;
			if (charCode != -33) // ignore spaces
			{
				if (charCode < 0 || charCode >= 26)
					return true; // Bad input
				startNum = startNum * 26 + charCode;
				anyDigits = true;
			}
			startStr = startStr.slice(1);
		}

		if (anyDigits)
			idNum = idNum + startNum + 1;
	}

	var idStr = "";
	while (idNum > 0)
	{
		var rem = idNum % 26;
		idNum = (idNum - rem) / 26;
		idStr = String.fromCharCode(65 + rem) + idStr;
	}
	idStr = padTo(idStr, padLen, "A");

	return prefixStr + dateTakenStr + idStr + getNewNameData.item.ext_m;
}

function padTo(str, len, prefix)
{
	str = str + "";
	while(str.length < len)
		str = prefix + str;
	return str;
}

function trimTo(str, len)
{
	str = str + "";
	if (str.length > len)
		str = str.substr(str.length - len, len);
	return str;
}

function OnGetCustomFields(fieldData)
{
	fieldData.fields.last_id = "";
	fieldData.field_labels("last_id") = "Last ID";
	fieldData.field_tips("last_id") = "Enter the used last ID. e.g. \"BA\" makes first file \"BB\".";
}

thanks for your time.

Thank you for not providing the complete preset.
Thank you for not linking to the original thread.
Thank you for not mentioning that you have already asked for this enhancement before.

Linking the previous thread for context: Alphabetic renaming

Wouldn't it be easier to use the normal YYYY-MM-DD HH-MM-SS date and time, based on the date-taken timestamps, in the file names at this point? That does what you want and is trivial to do and easier to read, and isn't that many extra characters.

Give this a try:

var lastDate = null;
var accumSub = 0;

function OnGetNewName(getNewNameData)
{
	var prefixStr = "RGC";
	var padLen = 2;

	var meta = getNewNameData.item.metadata.image;
	if (meta == null || meta.datetaken == null)
		return getNewNameData.item.name;
	var dateTakenDate = DOpus.Create.Date(meta.datetaken);
	var dateTakenStr = trimTo(dateTakenDate.year,2) + padTo(dateTakenDate.month,2,"0") + padTo(dateTakenDate.day,2,"0")

	var numStr = getNewNameData.newname;
	if (!/^(\d+)$/.test(numStr))
		return true; // Do not rename.
	var idNum = Number(numStr);

	if (lastDate == null)
		lastDate = DOpus.Create.Date(meta.datetaken);
	else {
		if (lastDate.day != dateTakenDate.day || lastDate.month != dateTakenDate.month || lastDate.year != dateTakenDate.year)
			accumSub = idNum;
		lastDate.Set(dateTakenDate);
	}
	idNum -= accumSub;	

	if (accumSub == 0) {
		var startStr = getNewNameData.custom.last_id;
		if (startStr)
		{
			var startNum = 0;
			var charCode;
			var anyDigits = false;
			startStr = startStr.toUpperCase();
			while(startStr.length > 0)
			{
				charCode = startStr.charCodeAt(0) - 65;
				if (charCode != -33) // ignore spaces
				{
					if (charCode < 0 || charCode >= 26)
						return true; // Bad input
					startNum = startNum * 26 + charCode;
					anyDigits = true;
				}
				startStr = startStr.slice(1);
			}
	
			if (anyDigits)
				idNum = idNum + startNum + 1;
		}
	}

	var idStr = "";
	while (idNum > 0)
	{
		var rem = idNum % 26;
		idNum = (idNum - rem) / 26;
		idStr = String.fromCharCode(65 + rem) + idStr;
	}
	idStr = padTo(idStr, padLen, "A");

	return prefixStr + dateTakenStr + idStr + getNewNameData.item.ext_m;
}

function padTo(str, len, prefix)
{
	str = str + "";
	while(str.length < len)
		str = prefix + str;
	return str;
}

function trimTo(str, len)
{
	str = str + "";
	if (str.length > len)
		str = str.substr(str.length - len, len);
	return str;
}

function OnGetCustomFields(fieldData)
{
	fieldData.fields.last_id = "";
	fieldData.field_labels("last_id") = "Last ID";
	fieldData.field_tips("last_id") = "Enter the used last ID. e.g. \"BA\" makes first file \"BB\".";
}

AlphaCount_4.zip (1.2 KB)

Thanks Leo, you're right in that your way is simpler and easier to read. Keeping the filename short is no advantage, few people actually types file names nowadays, it's drag-and-drop or copy-and-paste. The days of 8+3 filenames are gone. However Jon has provided a solution using alphanumeric so I'll trial both solutions. Much obliged for your help and for working without complaint with my poorly composed post.
kind regards, Robert

Jon, that works beautifully. Thank you. I don't understand why the script only occassionally prompts me for the previous alphabetic characters used but it doesn't stand in the way of using this marvellous rename script. Thanks again!
Robert

When you select the rename preset a field called "Last ID" should appear in the Rename dialog where you can enter the starting position. If you leave it empty it'll default to AA.

Thanks Jon. However "Last ID" only appears sometimes. If I select just one or four files, "Last ID" appears but if I select the entire directory, say 100 files, it doesn't. Attached are screen grabs to illustrate. What does the star to the left of the script name indicate? Sometimes it's yellow, sometimes not.
regards, Robert