How to keep the numbering inserted during the last use
Hello.
I rename images, assigning them a number that increments daily.
I use the `insert` command: `_img-<Inc Nr:00001>`.
I'm trying to figure out how to keep the last assigned number to continue numbering the next time.
I haven't been able to find a solution.
Thank you in advance for your help.
Sincerely,
I rename images, assigning them a number that increments daily.
I use the `insert` command: `_img-<Inc Nr:00001>`.
I'm trying to figure out how to keep the last assigned number to continue numbering the next time.
I haven't been able to find a solution.
Thank you in advance for your help.
Sincerely,
Reply to #1:
Welcome zebulion!
Unfortunately, Aren does not provide a way to store such information between runs.
So a kludgy workaround is to create a special file named "Last Number Used 07259.special".
Where 7259 is the last number you've used.
Then
1) Always include this in the list of files in your Aren batch.
2) Use a script method to do the numbering -- keying off of and resetting the value in the name of "Last Number Used nnnnn.special"
I don't have time right now, but will post an exact solution in several hours, if no one beats me to it.
Please confirm/provide:
A) These are new files, no file is getting renumbered.
B) Please provide exact before and after sample filenames.
Regards,
Randy
Welcome zebulion!
Unfortunately, Aren does not provide a way to store such information between runs.
So a kludgy workaround is to create a special file named "Last Number Used 07259.special".
Where 7259 is the last number you've used.
Then
1) Always include this in the list of files in your Aren batch.
2) Use a script method to do the numbering -- keying off of and resetting the value in the name of "Last Number Used nnnnn.special"
I don't have time right now, but will post an exact solution in several hours, if no one beats me to it.
Please confirm/provide:
A) These are new files, no file is getting renumbered.
B) Please provide exact before and after sample filenames.
Regards,
Randy
Reply to #2:
Hi Zeb, Randolph,
Another possible way to do it is to use the Unix timestamp to determine a baseline number of days (since 1970-01-01), then offset it by whatever number is needed to create your next sequence number. This will give you the day sequence in a script method:
const msPerDay = 24 * 60 * 60 * 1000;
const now = Date.now(); // ms since 1970-01-01 UTC
const daysSinceEpoch = Math.floor(now / msPerDay);
log( daysSinceEpoch );
Today (2026-06-17) the log would contain the number 20621. Tomorrow it would be 20622. So you'd have to offset that to create the next number in your sequence:
// ---------------------------------------------------------------------
//for instance, next number 00002:
const offset = 20619; //ADJUST TO YIELD SEQUENCE NUMBER
const msPerDay = 24 * 60 * 60 * 1000;
const now = Date.now(); // ms since 1970-01-01 UTC
const daysSinceEpoch = Math.floor(now / msPerDay);
// HERE IS WHERE YOU APPLY THE OFFSET:
retNumber = daysSinceEpoch - offset ;
retString = retNumber.toString(10);
return item.newBasename + "_" + retString.padStart( 5, "0" ) ;
// ---------------------------------------------------------------------
I ***think*** this works... Randy, thoughts?
[EDIT: Full disclosure, I used AI to get that first three lines, 'cause I'm too lazy to do it myself :) ]
Best,
DF
Hi Zeb, Randolph,
Another possible way to do it is to use the Unix timestamp to determine a baseline number of days (since 1970-01-01), then offset it by whatever number is needed to create your next sequence number. This will give you the day sequence in a script method:
const msPerDay = 24 * 60 * 60 * 1000;
const now = Date.now(); // ms since 1970-01-01 UTC
const daysSinceEpoch = Math.floor(now / msPerDay);
log( daysSinceEpoch );
Today (2026-06-17) the log would contain the number 20621. Tomorrow it would be 20622. So you'd have to offset that to create the next number in your sequence:
// ---------------------------------------------------------------------
//for instance, next number 00002:
const offset = 20619; //ADJUST TO YIELD SEQUENCE NUMBER
const msPerDay = 24 * 60 * 60 * 1000;
const now = Date.now(); // ms since 1970-01-01 UTC
const daysSinceEpoch = Math.floor(now / msPerDay);
// HERE IS WHERE YOU APPLY THE OFFSET:
retNumber = daysSinceEpoch - offset ;
retString = retNumber.toString(10);
return item.newBasename + "_" + retString.padStart( 5, "0" ) ;
// ---------------------------------------------------------------------
I ***think*** this works... Randy, thoughts?
[EDIT: Full disclosure, I used AI to get that first three lines, 'cause I'm too lazy to do it myself :) ]
Best,
DF
Reply to #2:
Thank you.
I was indeed thinking of a similar solution.
Since I'm only renaming photos, I think it will be fairly easy to follow.
Thanks again.
Best regards.
Thank you.
I was indeed thinking of a similar solution.
Since I'm only renaming photos, I think it will be fairly easy to follow.
Thanks again.
Best regards.
Reply to #3:
Hi DF,
The question is very ambiguous and I interpreted it differently. If he just wants to add *the same number* to all files for a given date, he would be smart just to use the date!
Also, with your script, as is, He needs to be excruciatingly careful about what he adds to the file list or hilarity will ensue. ;)
But it may well work for him.
My tea leaves told me his task was like:
Day 1:
A picture.jpg --> A picture_img-00001.jpg
B picture.jpg --> B picture_img-00002.jpg
Day 2:
A picture_img-00001.jpg *unchanged*
B picture_img-00002.jpg *unchanged*
C picture.jpg --> C picture_img-00003.jpg
D picture.jpg --> D picture_img-00004.jpg
etc.
Regards,
Randy
Hi DF,
The question is very ambiguous and I interpreted it differently. If he just wants to add *the same number* to all files for a given date, he would be smart just to use the date!
Also, with your script, as is, He needs to be excruciatingly careful about what he adds to the file list or hilarity will ensue. ;)
But it may well work for him.
My tea leaves told me his task was like:
Day 1:
A picture.jpg --> A picture_img-00001.jpg
B picture.jpg --> B picture_img-00002.jpg
Day 2:
A picture_img-00001.jpg *unchanged*
B picture_img-00002.jpg *unchanged*
C picture.jpg --> C picture_img-00003.jpg
D picture.jpg --> D picture_img-00004.jpg
etc.
Regards,
Randy
Reply to #3:
My fully "Nerd sniped" solution to my take on this problem is:
A) Use a batch file to capture a list of the previous files so that we may detect the last used number.
B) The batch file then calls Aren's command-line interface and renumbers the target files.
A great thing about this approach is that you can do the whole daily job with one double-click.
I have a few shortcuts on my desktop that call batch files -- that use Aren like this -- and it's pretty sweet.
Anywho,
1) Create a new script method ( https://www.advancedrenamer.com/user_guide/v4/method_script ).
2) Paste this next code in the "Pre batch script" and apply it:
//-------------------------------------------------------
// Pre batch script
//-------------------------------------------------------
const validBeforeNamePtn = /^.+?\.jpg$/;
const newNamePattern = /_img-\d{5}$/;
const stripExtPattern = /^(.+?)\.\w+$/;
const incNumberPattern = /-(\d+)$/;
const currWorkingDir = app.getItem(0).path;
let lastNumber = 1; // Will be updated below.
//-- This next file must have been updated by the batch file.
let dirListing = app.readFileText (currWorkingDir + "_imageList.txt");
if (typeof dirListing == "undefined") throw "Unable to read directory snapshot";
let filelines = dirListing.split (/[\n\r]+/);
filelines.forEach (zLine => {
let nameOnly = zLine.replace (stripExtPattern, "$1"); // We want only the file name here.
if (newNamePattern.test (nameOnly) ) {
let fileNumber = parseInt (nameOnly.split (incNumberPattern) [1], 10 );
if (fileNumber > lastNumber) {
lastNumber = fileNumber;
}
}
} );
console.log (`The last number from before was ${lastNumber}.`)
//-------------------------------------------------------
3) Then paste the following code in the "function(index, item)" block and apply it.
//-------------------------------------------------------
// "function( index, item)" script
//-------------------------------------------------------
/*------------------------------------------------------------
IMPORTANT! Run this method using the associated batch file
or the new file numbering will be wrong.
------------------------------------------------------------*/
if (validBeforeNamePtn.test(item.name + item.ext) && ! newNamePattern.test(item.name) ) {
lastNumber++;
let newName = item.name + "_img-" + String (lastNumber).padStart (5, '0');
return newName;
}
//-------------------------------------------------------
4) Now save the method as "NumberImagefiles.aren" and carefully note where you saved it.
EG: C:\my rename scripts\NumberImagefiles.aren
5) Using a text editor create a batch file named "NumberNewImgFiles.bat". Make sure that the extension really is ".bat".
For now, place it in the same folder as your image files.
This is the contents of that batch file:
@REM ------------------------------------------------
@ECHO OFF
@ECHO Renaming new image files, continuing from last used number
@ECHO Working Folder: %CD%
TITLE Renaming image files in %CD%
SETLOCAL
SET doneTxt=DONE
REM Capture the current image filenames in a file that Aren will read.
dir *.jpg /b > _imageList.txt
"C:\Program Files\Advanced Renamer\arenc.exe" -e "C:\my rename scripts\NumberImagefiles.aren" -p "%CD%" -r "^.*\.jpg$" -o modifieddate
IF %ERRORLEVEL% NEQ 0 (
COLOR 4E
SET doneTxt=ERROR!
ECHO ^^^^ See error message^(s^) above in the "Result" lines ^^^^
)
TITLE %doneTxt%: Renaming image files
ECHO.
PAUSE
REM ------------------------------------------------
To use, for now, double-click on the batch file.
Later you can add desktop shortcuts and even modify it to allow you to drag-and-drop folders.
Regards,
Randy
My fully "Nerd sniped" solution to my take on this problem is:
A) Use a batch file to capture a list of the previous files so that we may detect the last used number.
B) The batch file then calls Aren's command-line interface and renumbers the target files.
A great thing about this approach is that you can do the whole daily job with one double-click.
I have a few shortcuts on my desktop that call batch files -- that use Aren like this -- and it's pretty sweet.
Anywho,
1) Create a new script method ( https://www.advancedrenamer.com/user_guide/v4/method_script ).
2) Paste this next code in the "Pre batch script" and apply it:
//-------------------------------------------------------
// Pre batch script
//-------------------------------------------------------
const validBeforeNamePtn = /^.+?\.jpg$/;
const newNamePattern = /_img-\d{5}$/;
const stripExtPattern = /^(.+?)\.\w+$/;
const incNumberPattern = /-(\d+)$/;
const currWorkingDir = app.getItem(0).path;
let lastNumber = 1; // Will be updated below.
//-- This next file must have been updated by the batch file.
let dirListing = app.readFileText (currWorkingDir + "_imageList.txt");
if (typeof dirListing == "undefined") throw "Unable to read directory snapshot";
let filelines = dirListing.split (/[\n\r]+/);
filelines.forEach (zLine => {
let nameOnly = zLine.replace (stripExtPattern, "$1"); // We want only the file name here.
if (newNamePattern.test (nameOnly) ) {
let fileNumber = parseInt (nameOnly.split (incNumberPattern) [1], 10 );
if (fileNumber > lastNumber) {
lastNumber = fileNumber;
}
}
} );
console.log (`The last number from before was ${lastNumber}.`)
//-------------------------------------------------------
3) Then paste the following code in the "function(index, item)" block and apply it.
//-------------------------------------------------------
// "function( index, item)" script
//-------------------------------------------------------
/*------------------------------------------------------------
IMPORTANT! Run this method using the associated batch file
or the new file numbering will be wrong.
------------------------------------------------------------*/
if (validBeforeNamePtn.test(item.name + item.ext) && ! newNamePattern.test(item.name) ) {
lastNumber++;
let newName = item.name + "_img-" + String (lastNumber).padStart (5, '0');
return newName;
}
//-------------------------------------------------------
4) Now save the method as "NumberImagefiles.aren" and carefully note where you saved it.
EG: C:\my rename scripts\NumberImagefiles.aren
5) Using a text editor create a batch file named "NumberNewImgFiles.bat". Make sure that the extension really is ".bat".
For now, place it in the same folder as your image files.
This is the contents of that batch file:
@REM ------------------------------------------------
@ECHO OFF
@ECHO Renaming new image files, continuing from last used number
@ECHO Working Folder: %CD%
TITLE Renaming image files in %CD%
SETLOCAL
SET doneTxt=DONE
REM Capture the current image filenames in a file that Aren will read.
dir *.jpg /b > _imageList.txt
"C:\Program Files\Advanced Renamer\arenc.exe" -e "C:\my rename scripts\NumberImagefiles.aren" -p "%CD%" -r "^.*\.jpg$" -o modifieddate
IF %ERRORLEVEL% NEQ 0 (
COLOR 4E
SET doneTxt=ERROR!
ECHO ^^^^ See error message^(s^) above in the "Result" lines ^^^^
)
TITLE %doneTxt%: Renaming image files
ECHO.
PAUSE
REM ------------------------------------------------
To use, for now, double-click on the batch file.
Later you can add desktop shortcuts and even modify it to allow you to drag-and-drop folders.
Regards,
Randy
Reply to #6:
You're right, my friend. I didn't consider the implications of the <IncNr:> tag in Zeb's question.
Excellent solution!
Best,
DF
You're right, my friend. I didn't consider the implications of the <IncNr:> tag in Zeb's question.
Excellent solution!
Best,
DF
Reply to #7:
Thank you. And yours was an interesting approach as well.
Re: the implications of the <IncNr:> tag...
In fairness, that means little. Never ever trust a novice's attempted solution to their problem.
Especially when they don't provide sufficient details. Triply so when they refuse to answer basic questions.
There are whole treatises on this type of thing and it's one more reason why it's a shame we can't migrate Kim to a Stack Exchange derivative or Github.
Also, I'm taking bets on whether this user is an AI bot. There's been some sneaky ones lately and even one which Kim hasn't cleared out for over two weeks.
Apologies to zebulion, if he is in need of such. ;P
Regards,
Randy
Thank you. And yours was an interesting approach as well.
Re: the implications of the <IncNr:> tag...
In fairness, that means little. Never ever trust a novice's attempted solution to their problem.
Especially when they don't provide sufficient details. Triply so when they refuse to answer basic questions.
There are whole treatises on this type of thing and it's one more reason why it's a shame we can't migrate Kim to a Stack Exchange derivative or Github.
Also, I'm taking bets on whether this user is an AI bot. There's been some sneaky ones lately and even one which Kim hasn't cleared out for over two weeks.
Apologies to zebulion, if he is in need of such. ;P
Regards,
Randy
Reply to #8:
Well, thanks for trying to let me off the hook, but I should have read more carefully (a common failing now that I have very little to lose if I misread). :)
Oh, and as to hilarity ensuing, again, I should have thought that out a little more carefully as well. If a file already had a day number it could have another number tacked on; this fixes at least that problem: (starting at the line "retString = ..."
retString = "_" + retNumber.toString(10).padStart( 5, "0" ) ;
if ( /_\d+$/.test( item.newBasename ) ) {
retString = "" ;
}
return item.newBasename + retString ;
(Just in case anyone is thinking, "Wow, that's a great idea, add a day number to the filename...!") LOL
Personally I group files by date in a folder and apply the date to the windows created date of the folder, so no date needed in filename or foldername. Just have to be careful not to copy a folder, only move it.
Also I have to do it in Flash Renamer; the only thing I use that program for, but it does that one task flawlessly (credit where due).
Well, thanks for trying to let me off the hook, but I should have read more carefully (a common failing now that I have very little to lose if I misread). :)
Oh, and as to hilarity ensuing, again, I should have thought that out a little more carefully as well. If a file already had a day number it could have another number tacked on; this fixes at least that problem: (starting at the line "retString = ..."
retString = "_" + retNumber.toString(10).padStart( 5, "0" ) ;
if ( /_\d+$/.test( item.newBasename ) ) {
retString = "" ;
}
return item.newBasename + retString ;
(Just in case anyone is thinking, "Wow, that's a great idea, add a day number to the filename...!") LOL
Personally I group files by date in a folder and apply the date to the windows created date of the folder, so no date needed in filename or foldername. Just have to be careful not to copy a folder, only move it.
Also I have to do it in Flash Renamer; the only thing I use that program for, but it does that one task flawlessly (credit where due).