Change format of date within file name

Javascript renaming examples. Javascript renaming is supported in version 3 or newer.

Change format of date within file name

Postby sirrah » Sun Nov 22, 2020 12:14 pm

I am in the process of downloading loads of bank statements and invoices from various websites to reduce the amount of paper I have to file.
Statements for one bank, in particular, are downloaded with a filename like "Statement_05Apr2019.pdf" How can I change this to be 2019 04 05.pdf?

Thanks
sirrah
 
Posts: 1
Joined: Sun Nov 22, 2020 12:06 pm

Re: Change format of date within file name

Postby Admin » Mon Nov 23, 2020 12:27 am

BRU Javascript renaming function for this:

Code: Select all
Date.prototype.yyyymmdd = function() {
var mm = this.getMonth() + 1; // getMonth() is zero-based
var dd = this.getDate();

return [this.getFullYear(),
(mm>9 ? '' : '0') + mm,
(dd>9 ? '' : '0') + dd
].join(' ');
};

function getSecondPart(str) {
var index = str.indexOf("_")+1; // Gets the first index where a space occours
return name.substring(index);
}
function getFirstPart(str) {
var index = str.indexOf("_"); // Gets the first index where a space occours
return name.substring(0,index);
}

datetext = getSecondPart(name);

var d = new Date(Date.parse( datetext.substring(0,2)+" "+datetext.substring(2,5) +" "+datetext.substring(5,10) ));

newName = d.yyyymmdd();
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm


Return to Javascript Renaming