Subtract Hours from File Name

Advanced Renamer forum
#1 : 06/05-22 20:43
Ryan W
Ryan W
Posts: 3
I posted a question previously but was unable to find it via search. I have filenames with the following format:

YYYY-MM-DD_hh-mm-ss.######.jpg

I am attempting to subtract 5 hours from the time. I made notes and have the two followings scipts written down, but neither is working anymore. I get the error "Invalid Script: Invalid post script: uncaught: 'cannot read property 0 of null'

Script 1 from notes:

match = item.name.match(/^(\d{4}-\d{2}-\d{2})_(\d{2})-(\d{2})-(\d{2})(\.\d+)$/);
if (match[0]) {
epoch = Date.parse(match[1] + 'T' + match[2]+ ':' + match[3] + ':' + match[4]);
date = new Date(epoch - 5 * 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[5];
}

Script 2 from notes:

match = item.name.match(/^(\d{4}-\d{2}-\d{2})_(\d{2})-(\d{2})-(\d{2})$/);
if (match[0]) {
epoch = Date.parse(match[1] + 'T' + match[2]+ ':' + match[3] + ':' + match[4]);
date = new Date(epoch - 5 * 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);
}

Any help would be appreciated. Thanks!


06/05-22 20:43
#2 : 07/05-22 00:09
David Lee
David Lee
Posts: 1125
The error means that the .match() method has failed to match the pattern in the filename and so has returned match == Null. The script then fails when we try to read a non existent array element match[x].

You need to modify the regular expression to match your filenames and the following statement should do the trick:

match = item.name.match(/^(\d{4}-\d{2}-\d{2})_(\d{2})-(\d{2})-(\d{2})(\..*)/);

The if statement in line 3: "if (match[0]) {...}" was meant to catch errors when the match failed (and so leave the filename unchanged). However "match[0]" will cause exactly the same error so you should replace line 3 with: "if (match) {", which will correctly test for a null value.

The following is a more general script that will allow you to change a date in a filename with format YYYY-MM-DD_hh-mm-ss.###### by any number of YYYY, MM, DD, hh, mm or ss:

match = item.name.match(/^(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2})-(\d{2})(\..*)/);
if (match) {
Y = 1*match[1];
M = 1*match[2] - 1;
D = 1*match[3];
h = 1*match[4];
m = 1*match[5];
s = 1*match[6];
h -= 5;
date = new Date(Y, M, D, h, m, s);
Y = date.getFullYear();
M = date.getMonth() + 1;
D = date.getDate();
h = date.getHours();
m = date.getMinutes();
s = date.getSeconds();
return Y + "-" + ("0" + M).slice(-2)+ "-" + ("0" + D).slice(-2)
+ "_" + ("0" + h).slice(-2) + "-" + ("0" + m).slice(-2) + "-" + ("0" + s).slice(-2)
+ match[7];
}


07/05-22 00:09
#3 : 07/05-22 09:57
David Lee
David Lee
Posts: 1125
Reply to #1:
By the way... The arrangement of the Forum archive and the Search function are less than helpful! You can sometimes do better using a Google Advanced Search, limiting the search to "site or domain: https://www.advancedrenamer.com/forum_thread?&qu ot; If you are lucky some hits will return complete threads.

Both your original posts are archived in the document https://www.advancedrenamer.com/forum_thread However this document is just the list of original posts, not complete threads listed in a random order, and does not include the titles of the posts. You can find your own posts using the "search on page" function in your browser (search for "Ryan W") and your first post - dated 17/06-21 20:39 - is in the middle of the document.


07/05-22 09:57
#4 : 07/05-22 10:21
David Lee
David Lee
Posts: 1125
Reply to #1:
By the way... The arrangement of the Forum archive and the Search function are less than helpful! You can sometimes do better using a Google Advanced Search, limiting the search to "site or domain: https://www.advancedrenamer.com/forum_thread?&qu ot; If you are lucky some hits will return complete threads.

Both your original posts are archived in the document https://www.advancedrenamer.com/forum_thread However this document is just the list of original posts, not complete threads, and is seemingly listed in random order. Also it does not include the titles of the posts.

You can find your own posts on this page using the "search on page" function in your browser (search for "Ryan W") and your first post - dated 17/06-21 20:39 - is in the middle of the document.

The Forum archive of complete threads covers the past two years (back to May 2020) however your original post (which should appear on page 6 of the Forum archive) appears to be missing.


07/05-22 10:21