Reformat Date in File Name

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

Reformat Date in File Name

Postby mresau » Thu Jan 09, 2020 3:54 pm

Regex is far too complicated for me but I'm certain it can solve my issue.

I have file names that include a date written out.
Example "January 3, 2020".
I wish to change the format to YYYYMMDD ("20200103").

Any help is greatly appreciated.
mresau
 
Posts: 3
Joined: Thu Jan 09, 2020 3:40 pm

Re: Reformat Date in File Name

Postby therube » Thu Jan 09, 2020 7:52 pm

Don't know that it will be easy.
Will need multiple steps.


JavaScript renaming should handle it, as you could set up a table.
(Not that I know what a table in JS is ;-).)


With RegEx, as a start, pseudo-code...

1. i'd pad the day, even leaving the , at this point
so something like: 1:RegEx -> \s(\d,) -> 0\2
1, -> 01,
2, -> 02,
17, -> 17, (unchanged)

2. then you'd need to test each month, separately
(January) -> 01
(February) -> 02
(March) -> 03 ...

3. with that you'd have some like:
01 01, 2020
01 17, 2020
02 01, 2020

4. once you have that you can then test for that & rearrange things,
& that RegEx would handle:
(\d\d\)\s(\d\d),\s(\d\d\d\d) -> \3\1\2


The bugger is the conversion of the month in 2.
therube
 
Posts: 886
Joined: Mon Jan 18, 2016 6:23 pm

Re: Reformat Date in File Name

Postby Admin » Fri Jan 10, 2020 3:03 am

Hi, can you give a few file names as example? Is there are a way to identify which part of the file name is the date, e.g. always at the end after a space or other criteria. thanks
Admin
Site Admin
 
Posts: 1848
Joined: Tue Mar 08, 2005 8:39 pm

Re: Reformat Date in File Name

Postby mresau » Fri Jan 10, 2020 2:52 pm

Thank you for the responses.

First, I run the files through a content renaming software so there is some opportunity to create the name in a way that makes it easy to get to this string. I just can't change the data from the content reading. As it stands the file name is "AccountNumber Date Written Out"
Example "1234567890 January 6, 2020.pdf"
So, as it stands there is always a set number of digits and a space prior to the date string.

Again, to create a launching pad for the program to seek out would be simple. I could add spaces, general symbols, a word, etc. Pretty much whatever needs to be done to make it easier.

I figured the stumbling point would be the conversion of the month to the numeric representation.
mresau
 
Posts: 3
Joined: Thu Jan 09, 2020 3:40 pm

Re: Reformat Date in File Name

Postby Admin » Fri Jan 10, 2020 11:07 pm

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) {
    return str.split(' ')[1];
}

function getFirstPart(str) {
    return str.split(' ')[0];
}

var d = Date.parse(getSecondPart(name));
newName = getFirstPart(name) + ' ' + d.yyyymmdd();



This Javascript code will change

1234567890 January 6, 2020.pdf

into

1234567890 20200106.pdf

It will assume that the date always starts after the first space.

You can use this script in Special (14) -> Javascript renaming
Admin
Site Admin
 
Posts: 1848
Joined: Tue Mar 08, 2005 8:39 pm

Re: Reformat Date in File Name

Postby mresau » Sat Jan 11, 2020 1:18 pm

Amazing! This works exactly as I needed!

Thank you!
mresau
 
Posts: 3
Joined: Thu Jan 09, 2020 3:40 pm

Re: Reformat Date in File Name

Postby trm2 » Wed Jan 15, 2020 12:54 pm

Hello.

I copied and pasted your code but I get an error when I test it.

---------------
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();
-------------------

Javascript Error:
TypeError: undefined is not a function in : newName = getFirstPart(name) + '' + d.yyyymmddQ; [Line:20 Start:39 End:40]

Please assist.
trm2
 
Posts: 154
Joined: Wed Jan 15, 2020 12:47 pm

Re: Reformat Date in File Name

Postby therube » Wed Jan 15, 2020 2:15 pm

At the least, the error message you quoted does not agree with the source?

d.yyyymmdd();
vs
d.yyyymmddQ;

Don't know why that is?
(The source you quoted is correct.)
therube
 
Posts: 886
Joined: Mon Jan 18, 2016 6:23 pm

Re: Reformat Date in File Name

Postby Admin » Thu Jan 16, 2020 12:02 am

Hi, make sure the code is correct , the error message reported "yyyymmddQ" as error.
Admin
Site Admin
 
Posts: 1848
Joined: Tue Mar 08, 2005 8:39 pm

Re: Reformat Date in File Name

Postby trm2 » Fri Jan 17, 2020 2:18 am

Sorry - my Screen reader messed up on error - Here is corrected error:

Javascript Error:
TypeError: undefined is not a function in : newName = getFirstPart(name) + '' + d.yyyymmdd(); [Line:20 Start:39 End:40]

Hope that helps to clarify.

Source is exact as entered (no screen reader just copy and paste). I have just gotten the JavaScript license so I don't know how to fix the problem.

Please assist. Thank you.
trm2
 
Posts: 154
Joined: Wed Jan 15, 2020 12:47 pm

Re: Reformat Date in File Name

Postby trm2 » Mon Jan 20, 2020 1:33 am

I do not understand why I am getting this error if others are not.

I have copied and pasted the code now about 10 times.
trm2
 
Posts: 154
Joined: Wed Jan 15, 2020 12:47 pm

Re: Reformat Date in File Name

Postby Admin » Mon Jan 20, 2020 3:35 am

Hi , make sure the library date.js is included : menu -> special -> javascript libraries -> date.js
Admin
Site Admin
 
Posts: 1848
Joined: Tue Mar 08, 2005 8:39 pm

Re: Reformat Date in File Name

Postby trm2 » Mon Jan 20, 2020 5:51 am

Thank you for that. Now I get a different error message which hopefully will provide a better clue to the problem.:

Javascript Error:
TypeError: Cannot read property 'yyyymmdd' of null in : newName getFirstPart(name) + '' + d.yyyymmdd(); [Line:20 Start:38 End:39]
trm2
 
Posts: 154
Joined: Wed Jan 15, 2020 12:47 pm

Re: Reformat Date in File Name

Postby Admin » Mon Jan 20, 2020 6:18 am

Are you using the latest version / build ? thanks
Admin
Site Admin
 
Posts: 1848
Joined: Tue Mar 08, 2005 8:39 pm

Re: Reformat Date in File Name

Postby trm2 » Mon Jan 20, 2020 2:48 pm

Yes. v3.3.1.0 64 bit Unicode. Do not understand this. If it was a bad copy, I would believe other functions of the program would be affected.


Other JavaScript samples seemed to test fine (thanks for the heads up about the libraries - had not noticed that I forgot to include them back)


This one tests fine:

var today = new Date();
var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date +' '+time;

newName = name + '-' + dateTime;


and This one tests fine:

var sep = "-";
var datestr = name.substring(0, 10);
var rest = name.substring(10);
var parts = datestr.split(sep);

if (parts.length > 2) {
newName = parts[2] + sep + parts[1] + sep + parts[0] + rest;
}

In fact, I went through ALL of the JavaScript examples available on the two pages of the JavaScript forum and they all tested fine (just to make sure my program was working) with the exception of this
one:

Entered as a condition in Filters

object('modified').getTime() != exif('%d').getTime() && exif('%d').getTime() != 0 in Filters (12) -> Condition

generated this error:

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]


BTW - if you know what caused that error, that would help too.

I really would like to get your code working. I looks like it would make a great addition to something I am working on and it looks extremely useful!
trm2
 
Posts: 154
Joined: Wed Jan 15, 2020 12:47 pm

Next

Return to Javascript Renaming