Rename part of file name from dd-mm-yyyy to yyyy-mm-dd

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

Rename part of file name from dd-mm-yyyy to yyyy-mm-dd

Postby Admin » Tue May 08, 2018 2:31 am

To rename :

03-01-2012_U0-52dc.pdf

which is [dd-mm-yyyy]

into

2012-01-03_U0-52dc.pdf

which is [yyyy-mm-dd]

Use the following Javascript code in BRU:

Code: Select all
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;
}
Admin
Site Admin
 
Posts: 1728
Joined: Tue Mar 08, 2005 8:39 pm

Re: Rename part of file name from dd-mm-yyyy to yyyy-mm-dd

Postby Carol-O » Tue May 08, 2018 9:46 am

:D Great !
Carol-O
 
Posts: 1
Joined: Mon May 07, 2018 10:42 am

Re: Rename part of file name from dd-mm-yyyy to yyyy-mm-dd

Postby matejmeglic » Tue Jan 14, 2020 3:46 pm

In case someone needs to organize files with different date formats yyyy-d-m vs yyyy-dd-m vs yyyy-d-mm vs yyyy-dd-mm into yyyy_mm_dd, here is the solution.

Code: Select all


// declare
var sep = "_";
var years = "";
var days = "";
var months = "";
var rest = "";


// yyyy-dd-mm-
if (name.substring(7,8) === "_" && name.substring(10,11) === "_") {
years = name.substring(0, 4);
days = name.substring(5, 7);
months = name.substring(8, 10);
rest = name.substring(11,400);

newName=  years + sep + months + sep + days + sep + rest;

}
// yyyy-d-m-
else if (name.substring(6,7) === "_" && name.substring(8,9) === "_") {
years = name.substring(0, 4);
days = name.substring(5, 6);
months = name.substring(7, 8);
rest = name.substring(9,400);

newName=  years + sep + "0" + months + sep + "0" + days + sep + rest;

}
// yyyy-dd-m-
else if (name.substring(7,8) === "_") {

years = name.substring(0, 4);
days = name.substring(5, 7);
months = name.substring(8, 9);
rest = name.substring(10,400);

newName=  years + sep + "0" + months + sep + days + sep + rest;

}
// yyyy-d-mm-
else if (name.substring(6,7) === "_") {
years = name.substring(0, 4);
days = name.substring(5, 6);
months = name.substring(7, 9);
rest = name.substring(10,400);

newName=  years + sep +  months + sep + "0" + days + sep + rest;

}
matejmeglic
 
Posts: 3
Joined: Mon Jan 13, 2020 8:59 pm

Re: Rename part of file name from dd-mm-yyyy to yyyy-mm-dd

Postby matejmeglic » Tue Jan 14, 2020 6:42 pm

Code: Select all
// declare
var sep = "_";
var years = "";
var days = "";
var months = "";
var rest = "";


// dd-mm-yyyy
if (name.substring(2,3) === "." && name.substring(5,6) === ".") {
years = name.substring(6, 10);
days = name.substring(0, 2);
months = name.substring(3, 5);
rest = name.substring(10,400);

newName=  years + sep + months + sep + days + sep + rest;

}

// d-m-yyyy
else if (name.substring(1,2) === "." && name.substring(3,4) === ".") {
years = name.substring(4, 8);
days = name.substring(0, 1);
months = name.substring(2,3);
rest = name.substring(8,400);

newName=  years + sep +"0"+ months + sep +"0"+ days + sep + rest;

}

// d-mm-yyyy
else if (name.substring(1,2) === ".") {
years = name.substring(5, 9);
days = name.substring(0, 2);
months = name.substring(2, 4);
rest = name.substring(9,400);

newName=  years + sep + months + sep + "0"+ days + sep + rest;

}

// dd-m-yyyy
else if (name.substring(2,3) === ".") {
years = name.substring(5, 9);
days = name.substring(0, 2);
months = name.substring(3, 4);
rest = name.substring(9,400);

newName=  years + sep + "0"+ months + sep + days + sep + rest;

}


simmilar example with dots d.m.yyyy
matejmeglic
 
Posts: 3
Joined: Mon Jan 13, 2020 8:59 pm


Return to Javascript Renaming