Add or substract X hours from filename

Advanced Renamer forum
#1 : 09/06-21 05:49
Nina
Nina
Posts: 3
Hello,

I read David Lees reply to the post at https://www.advancedrenamer.com/forum_thread?for um_id=11037, and I've been trying for hours to adjust the syntax to my filename:

I would like to turn
PXL_20210605_105152883.dng
into
2021.06.05, 12.51.52,883.dng

The original script was:

match = item.name.match(/^(.*_)(\d{4}-\d{2}-\d{2})_(\d{2})-(\d{2})-(\d{2})$/);
epoch = Date.parse(match[2] + 'T' + match[3] + ':' + match[4] + ':' + match[5]);
date = new Date(epoch + 7 * 3600000);
return match[1]
+ date.getFullYear()
+ "-" + ("0" + (date.getMonth() + 1)).slice(-2)
+ "-" + ("0" + date.getDate()).slice(-2)
+ "_" + ("0" + date.getHours()).slice(-2)
+ "-" + ("0" + date.getMinutes()).slice(-2)
+ "-" + ("0" + date.getSeconds()).slice(-2);

I either get an error in the Error-Column, or an Error Window "Access violation at address 68C5F7B5 in module js32.dll."

Could anyone point me to a resource that teaches how to change the syntax. Thank you so much

Nina


09/06-21 05:49
#2 : 09/06-21 05:59
Nina
Nina
Posts: 3
Uh,

I forgot... Some files are called a little bit different:

PXL_20210605_153935016.mp4
PXL_20210604_234852214.NIGHT.dng
PXL_20210605_105211801.jpg

I hope, that's not an issue? I wish there was a way to just tell the script to use position and length, like with the "<Substr:5:4>"-Tag!

Thanks again
Nina


09/06-21 05:59
#3 : 09/06-21 07:15
Nina
Nina
Posts: 3
I am now learning about regex :D

I'm at:

match = item.name.match(/^(.*_)(\d{4}\d{2}\d{2})_(\d{2}\d{2}\d{2}).*$/);
epoch = Date.parse(match[2] + 'T' + match[3] + ':' + match[4] + ':' + match[5]);
date = new Date(epoch + 2 * 3600000);
return match[1]
+ date.getFullYear()
+ "-" + ("0" + (date.getMonth() + 1)).slice(-2)
+ "-" + ("0" + date.getDate()).slice(-2)
+ "_" + ("0" + date.getHours()).slice(-2)
+ "-" + ("0" + date.getMinutes()).slice(-2)
+ "-" + ("0" + date.getSeconds()).slice(-2);

for the filenames:

PXL_20210605_105152883.dng
PXL_20210605_153935016.mp4
PXL_20210604_234852214.NIGHT.dng
PXL_20210605_105211801.jpg

It still doesn't work : (

Nina


09/06-21 07:15
#4 : 09/06-21 09:42
David Lee
David Lee
Posts: 717
Reply to #3:

You're getting closer!

However you have made several errors.

Firstly your regex is only capturing three groups - so it returns:
match[1] = "PXL_"
match[2] = "20210605"
match[3] = "105152"

This means that the program is trying to parse the date string:
"20210605T105152:undefined:undefined"

An ISO date should have the format: "2021-06-05T10:51:52"
so you need to match yyyy, mm, dd, hh, mm & ss separately.

Initially you indicated that you do not want to retain the prefix ("PXL_") so you don't need to capture the first match. However you do need to match and capture the milliseconds at the end of the string.

So use the regex: /^.*_(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})(\d{3})/

The date.get methods will return results in your local timezone. This will include daylight saving, where applicable. Since the epoch has been calculated assuming UTC you should replace these methods with the date.getUTC equivalents to avoid errors due to summer time.

Finally, add the milliseconds that we captured as match[7] (ie decimal seconds).

The following script should work for you...


match = item.name.match(/^.*_(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})(\d{3})/);
epoch = Date.parse(match[1] + '-' + match[2]+ '-' + match[3] + 'T' + match[4] + ':' + match[5] + ':' + match[6]);
date = new Date(epoch + 2 * 3600000);
return date.getUTCFullYear()
+ "." + ("0" + (date.getUTCMonth() + 1)).slice(-2)
+ "." + ("0" + date.getUTCDate()).slice(-2)
+ "_" + ("0" + date.getUTCHours()).slice(-2)
+ "." + ("0" + date.getUTCMinutes()).slice(-2)
+ "." + ("0" + date.getUTCSeconds()).slice(-2)
+ "," + match[7];





09/06-21 09:42