Reformat Date in File Name

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

Re: Reformat Date in File Name

Postby trm2 » Mon Jan 20, 2020 8:23 pm

As far as the condition error - before you ask :) ...

I have the Extract EXIF data set in Renaming Options so that isn't the problem.

So please help with 2 errors thank you in advance.
trm2
 
Posts: 155
Joined: Wed Jan 15, 2020 12:47 pm

Re: Reformat Date in File Name

Postby trm2 » Wed Jan 22, 2020 6:05 pm

Looking at the date code (with no expertise). If I remove the renaming line (last line) and test it, the code tests ok:

---------------------
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) {
return str.split(' ')[1];
}
function getFirstPart(str) {
return str.split(' ')[0];
}
var d = Date.parse(getSecondPart(name));
newName = getFirstPart(name) + ' ' + d.yyyymmdd();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) {
return str.split(' ')[1];
}
function getFirstPart(str) {
return str.split(' ')[0];
}
var d = Date.parse(getSecondPart(name));

-----------------

So this tells me it is not a syntax error as far as the above code. When I add the renaming code-

newName = getFirstPart(name) + ' ' + d.yyyymmdd();

Then it generates the error. Does that help you in evaluating my problem??
trm2
 
Posts: 155
Joined: Wed Jan 15, 2020 12:47 pm

Re: Reformat Date in File Name

Postby Admin » Thu Jan 23, 2020 2:40 am

Hi , regarding this one:

Condition Error
Object: 1863_22_10_2019
SyntaxError: Unexpected token > in : exif('%d‘).getTime()&& exif('%d‘).getTime() != 0 in Filters (12) -> Conditio [Lined Start:68 End:69]


The condition should be just :
Code: Select all
object('modified').getTime() != exif('%d').getTime() && exif('%d').getTime() != 0
Admin
Site Admin
 
Posts: 1896
Joined: Tue Mar 08, 2005 8:39 pm

Re: Reformat Date in File Name

Postby Admin » Thu Jan 23, 2020 3:21 am

Regarding the other javascript function...

Does this version work for you ?

Code: Select all
function getSecondPart(str) {
   var index = str.indexOf(" ");  // 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);
}

var d = new Date(Date.parse(getSecondPart(name)));

var mm = d.getMonth() + 1; // getMonth() is zero-based
var dd = d.getDate();

var yyyymmdd =  [d.getFullYear(),
          (mm>9 ? '' : '0') + mm,
          (dd>9 ? '' : '0') + dd
         ].join('');

newName = getFirstPart(name) + ' ' + yyyymmdd;


thanks
Admin
Site Admin
 
Posts: 1896
Joined: Tue Mar 08, 2005 8:39 pm

Re: Reformat Date in File Name

Postby Admin » Thu Jan 23, 2020 3:26 am

And this?

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(" "); // 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);
}

var d = new Date(Date.parse(getSecondPart(name)));

newName = getFirstPart(name) + ' ' + d.yyyymmdd();
Admin
Site Admin
 
Posts: 1896
Joined: Tue Mar 08, 2005 8:39 pm

Re: Reformat Date in File Name

Postby Admin » Thu Jan 23, 2020 3:28 am

I think there is an issue with the first javascript function we posted we some file names with extra spaces...
Admin
Site Admin
 
Posts: 1896
Joined: Tue Mar 08, 2005 8:39 pm

Re: Reformat Date in File Name

Postby trm2 » Thu Jan 23, 2020 4:37 am

YES! Thank you on both accounts -

The Condition - I pasted it - and my unfamiliarity with JavaScript did not catch the added 'Filter 12' that merely identified where to place it. I would have caught that
easy enough if it had been part of a RegEx expression though :) My ignorance thanks you.

And both versions of the javascript code passed the test! Again Thank you!
trm2
 
Posts: 155
Joined: Wed Jan 15, 2020 12:47 pm

Re: Reformat Date in File Name

Postby trm2 » Thu Jan 23, 2020 4:52 am

Just tested it on the sample file and both work as expected.

Question do you have a routine to perform the opposite function?

i.e. 1234567890 20200106.pdf to 1234567890 January 6, 2020.pdf

Thanks again!
trm2
 
Posts: 155
Joined: Wed Jan 15, 2020 12:47 pm

Re: Reformat Date in File Name

Postby Admin » Wed Feb 05, 2020 1:06 am

Admin
Site Admin
 
Posts: 1896
Joined: Tue Mar 08, 2005 8:39 pm

Previous

Return to Javascript Renaming