Set newModifiedDate in Script

Advanced Renamer forum
#1 : 30/08-20 06:51
Bruno G.
Bruno G.
Posts: 6
I want to update the file's timestamp according to it's name, but some parts of the date might be missing so I can't use the Timestamp method.

Sample file names with expected timestamp:
FileWithNoDate.txt (Leave current date)
2019 YearOnly.txt (2019-12-31)
2019-03 YearAndMonth.txt (2019-03-31)
2019-03-26 FullDate.txt (2019-03-26)

So I made this script :
- - - - - - - - - - - - - - - - - - - - - - - -
//Current date & time
var d = new Date();

//Extract date info from filename
var data = item.filename.match(/^(\d{4})?(?:-(\d{2}))?(?:-(\d{2}))?/);

//No year, leave file as is
if (data[1] === undefined) { return; }

//No month, we set timestamp to Dec 31st
if (data[2] === undefined) {
d.setFullYear(data[1], 11, 31);

//No day, we set it to the last day of the month
} else if (data[3] === undefined) {
//Month is zero-based, so for example, July is 6
//We set to day 0 of month, so it's day 0 of month 7 (that's day 0 of August, ie July 31st)
d.setFullYear(data[1], data[2], 0);

//Full date is present, we use that
} else {
// -1 because month is zero-based
d.setFullYear(data[1], data[2]-1, data[3]);
}

//We set all dates
item.newCreatedDate = d;
item.newModifiedDate = d;
item.newAccessedDate = d;
- - - - - - - - - - - - - - - - - - - - - - - -

But the file's dates are not updated. What am I missing?


30/08-20 06:51
#2 : 30/08-20 09:39
David Lee
David Lee
Posts: 1125
item.filename returns the original name including the full path (and file extension).

You need to use item.name to return the original name only (or item.newName if you have previously changed the name in the same batch, but note that newName also includes the extension).

Try using app.log() to debug your scripts

The JS Console was introduced in the latest version of ARen and Kim hasn't yet updated the User Guide to include it. It's worth checking the "What's New" section of the website to see info about undocumented features such as this: www.advancedrenamer.com/whatsnew



30/08-20 09:39 - edited 30/08-20 12:42
#3 : 31/08-20 14:53
Bruno G.
Bruno G.
Posts: 6
Reply to #2:
Doh! Thanks for the tip.

So, I've fixed the issues in my code and confirmed that I get the expected date with app.log() — I also noticed that the "New Created Date" column in the "Rename Files" list does display the expected date.

But the file still isn't updated.

This can be tested easily with the following, simplified code:

var d = new Date();
d.setFullYear(2019, 0, 30);
item.newCreatedDate = d;

Again, the "New Created Date" column shows the expected date (Jan. 30th 2019), but "Start batch" fails to actually update the file's timestamp.


31/08-20 14:53
#4 : 31/08-20 18:39
David Lee
David Lee
Posts: 1125
Reply to #3:
I've encountered that problem as well!

A work-around is to modify the filename temporarily so that you can use the Filename pattern in the Timestamp method.

You will need to use two passes since the Timestamp method will use the ORIGINAL filename.

Pass 1:
List Replace method...
Replace: "(^\d{4}) " with: "\1-12-31!\1 "
Replace: "(^\d{4}-\d{2}) " with: "\1-31!\1 "
Replace: "(^\d{4}-\d{2}-\d{2}) " with: "\1!\1 "
Use regular expressions

Pass 2 (two methods):
1) Timestamp method...
Filename pattern
Pattern: <Year>-<Month>-<Day>!

2) Replace method...
Replace: "^.*!"
with: blank
Use regular expressions

When you run the second batch, files with no date will throw Error "(118) Pattern not found" - just click "Skip erroneous files" to continue.


31/08-20 18:39 - edited 31/08-20 20:48