Adding time to timestamp
I have files which have a date/time and an index at the end of each filename. e.g.
2026_02_03__18_13_53_0.jpg
2026_02_03__18_13_53_1.jpg
2026_02_03__18_13_53_2.jpg
2026_02_03__18_13_53_3.jpg
I'd like to set the created and modified timestamps to the time in the filename and then add the index to the seconds so they'll show up in Explorer in the correct order.
So the timestamps of the files would be:
2026-02-03 18:13:53
2026-02-03 18:13:54
2026-02-03 18:13:55
2026-02-03 18:13:56
I know you should be able to do this pretty easily in a script, but it's been a long time since I did any scripting. I've tried to use what I can find on the forum, but I can't seem to get it to work.
If any of you scripting experts could create an example, it would be greatly appreciated.
2026_02_03__18_13_53_0.jpg
2026_02_03__18_13_53_1.jpg
2026_02_03__18_13_53_2.jpg
2026_02_03__18_13_53_3.jpg
I'd like to set the created and modified timestamps to the time in the filename and then add the index to the seconds so they'll show up in Explorer in the correct order.
So the timestamps of the files would be:
2026-02-03 18:13:53
2026-02-03 18:13:54
2026-02-03 18:13:55
2026-02-03 18:13:56
I know you should be able to do this pretty easily in a script, but it's been a long time since I did any scripting. I've tried to use what I can find on the forum, but I can't seem to get it to work.
If any of you scripting experts could create an example, it would be greatly appreciated.
Hi Dave,
Here's a script to do exactly what you asked (although depending on your times and increments it may not be what you actually want). If this does it for you, though, GREAT:
//-----------------------------------------
name = item.newBasename;
n1 = parseInt( name.replace(/.*(\d+$)/, "$1" ) ) ;
n2 = parseInt( name.replace(/.*_(\d+)_\d+$/, "$1" ) ) ;
nn = name.replace( /^(.*)_\d+_\d+$/, "$1_" + ( n1 + n2 ).toString() ) ;
return nn ;
//-----------------------------------------
If you have files where seconds + increment total more than 60, this won't roll over to the next higher minute. That's not a hard problem to solve, and I started on a way to do it, but I'm kind of under the weather today and can't really concentrate. My solution is brute-force and not pretty, but it should work up to where you have totals greater than 120. Randy can probably do a prettier job (Randy, watch out for trucks! :) but if you want to see how it *could* be done let me know. Maybe I'll feel better and work on it some more...
Over a year ago (I think), I did a script for someone with stopwatch timings summing, where everything rolled over to the next higher second, minute, and hour, but I can't seem to find it now using the forum search. You might be able to find it using google search, but I don't remember if it would actually apply to what you are doing here. I'm sure I have it in my backups and I'll try to find it at some point.
Best,
DF
Here's a script to do exactly what you asked (although depending on your times and increments it may not be what you actually want). If this does it for you, though, GREAT:
//-----------------------------------------
name = item.newBasename;
n1 = parseInt( name.replace(/.*(\d+$)/, "$1" ) ) ;
n2 = parseInt( name.replace(/.*_(\d+)_\d+$/, "$1" ) ) ;
nn = name.replace( /^(.*)_\d+_\d+$/, "$1_" + ( n1 + n2 ).toString() ) ;
return nn ;
//-----------------------------------------
If you have files where seconds + increment total more than 60, this won't roll over to the next higher minute. That's not a hard problem to solve, and I started on a way to do it, but I'm kind of under the weather today and can't really concentrate. My solution is brute-force and not pretty, but it should work up to where you have totals greater than 120. Randy can probably do a prettier job (Randy, watch out for trucks! :) but if you want to see how it *could* be done let me know. Maybe I'll feel better and work on it some more...
Over a year ago (I think), I did a script for someone with stopwatch timings summing, where everything rolled over to the next higher second, minute, and hour, but I can't seem to find it now using the forum search. You might be able to find it using google search, but I don't remember if it would actually apply to what you are doing here. I'm sure I have it in my backups and I'll try to find it at some point.
Best,
DF
Reply to #1:
Hi Dave,
Here's another way to do it with a javascript renaming method. (https://www.advancedrenamer.com/user_guide/v4/method_script )
It has error checking and rollover if the sequence is >= 60
//-- Convert from "YYYY_MM_DD__HH_mm_ss_k" to "YYYY-MM-DDTHH:mm:ss" format (k is a sequencer here)
let stdTimeStr = item.newBasename.replace (
/^.*?(\d{4})[_-](\d{2})[_-](\d{2})[_-]+(\d{2})[_-](\d{2})[_-](\d{2})[_-].+/, "$1-$2-$3T$4:$5:$6"
);
let newTimeStamp = new Date (stdTimeStr);
if (isNaN (newTimeStamp.getTime () ) ) {
return "ERROR IN TIME FORMAT";
}
//-- Now add sequence offset, note that sequence values >= 60 will roll into minutes/hours as necessary
let seqStr = item.newBasename.replace (/^.+?_(\d+)$/, "$1");
let seqValue = parseInt (seqStr, 10);
newTimeStamp.setSeconds (newTimeStamp.getSeconds() + seqValue);
//-- Set the actual file dates
item.newModifiedDate = newTimeStamp;
item.newCreatedDate = newTimeStamp;
^^^ Beware that this forum wraps and justifies the *appearance* of the code. Copy and paste, from here, still usually gets it right.
Regards,
Randy
PS to DF: I think he wanted to change timestamps, not the name. Perhaps I misread? And thank you for the heads up on those sneaky vehicles. ;P
Hi Dave,
Here's another way to do it with a javascript renaming method. (https://www.advancedrenamer.com/user_guide/v4/method_script )
It has error checking and rollover if the sequence is >= 60
//-- Convert from "YYYY_MM_DD__HH_mm_ss_k" to "YYYY-MM-DDTHH:mm:ss" format (k is a sequencer here)
let stdTimeStr = item.newBasename.replace (
/^.*?(\d{4})[_-](\d{2})[_-](\d{2})[_-]+(\d{2})[_-](\d{2})[_-](\d{2})[_-].+/, "$1-$2-$3T$4:$5:$6"
);
let newTimeStamp = new Date (stdTimeStr);
if (isNaN (newTimeStamp.getTime () ) ) {
return "ERROR IN TIME FORMAT";
}
//-- Now add sequence offset, note that sequence values >= 60 will roll into minutes/hours as necessary
let seqStr = item.newBasename.replace (/^.+?_(\d+)$/, "$1");
let seqValue = parseInt (seqStr, 10);
newTimeStamp.setSeconds (newTimeStamp.getSeconds() + seqValue);
//-- Set the actual file dates
item.newModifiedDate = newTimeStamp;
item.newCreatedDate = newTimeStamp;
^^^ Beware that this forum wraps and justifies the *appearance* of the code. Copy and paste, from here, still usually gets it right.
Regards,
Randy
PS to DF: I think he wanted to change timestamps, not the name. Perhaps I misread? And thank you for the heads up on those sneaky vehicles. ;P
Reply to #3:
Yes, you're right... I just shortcut the process and assumed Dave would know to set the date/times with a timestamp method. Probably should have mentioned that, right? I'm not really thinking all that clearly today. Even less clearly than most days... Anyway, I figured you'd come along and save the day! :)
Best,
DF
Yes, you're right... I just shortcut the process and assumed Dave would know to set the date/times with a timestamp method. Probably should have mentioned that, right? I'm not really thinking all that clearly today. Even less clearly than most days... Anyway, I figured you'd come along and save the day! :)
Best,
DF
Reply to #4:
None of that. Go outside and take a constitutional, my friend. (^_^)
None of that. Go outside and take a constitutional, my friend. (^_^)
Wow!! Thanks so much guys! Randy, your script worked perfectly, except I needed to change the last line to item.newCreatedDate to set the created date. Yes, I only wanted to change the timestamps, not the filenames. They are already in the correct format.
I wish I would have learned about Advanced Renamer years ago. It's saving me so much work manipulating filenames/timestamps! It's now my most useful utility so I purchased a license.
I really need to learn more JavaScript! There's probably not much the scripting function can't do if you know how to write code.
Thanks again and DF, I hope you feel better!
I wish I would have learned about Advanced Renamer years ago. It's saving me so much work manipulating filenames/timestamps! It's now my most useful utility so I purchased a license.
I really need to learn more JavaScript! There's probably not much the scripting function can't do if you know how to write code.
Thanks again and DF, I hope you feel better!
Reply to #6:
>> I needed to change the last line to item.newCreatedDate to set the created date.
Oops! Good catch. (serves me right for not adding that column to the file list display) .
I've added the correction to my original post.
Glad you got it working; have a great day!
>> I needed to change the last line to item.newCreatedDate to set the created date.
Oops! Good catch. (serves me right for not adding that column to the file list display) .
I've added the correction to my original post.
Glad you got it working; have a great day!