Convert file name from xyz_10JUL2018.csv to xyz_20180710.csv

Seven Stars

Convert file name from xyz_10JUL2018.csv to xyz_20180710.csv

Suppose i have multiple files in a folder named as 

 

xyz_10JUL2018.csv

abc_05JUN2017.csv

mnc_15APR2018.csv

 

I wanted to convert those files into 

xyz_20180710.csv

abc_20170605.csv

mnc_20180415.csv

 

Please suggest your optimized thoughts and how i can achieve this using less number of components.

 

Regards 

Meet 


Accepted Solutions
Forteen Stars TRF
Forteen Stars

Re: Convert file name from xyz_10JUL2018.csv to xyz_20180710.csv

@iamabhishek gave you the right direction regarding the general job design, but nothing about the date conversion which finally seems to be the main part of the question.

I assume the filename pattern is always the same:

  • some characters terminated by an underscrore "_"
  • the date formatted as ddMMMyyyy
  • the file extension introduced by a dot "."

A simple solution is to use the "String.split()" method to extract each part from the filename, then change the date format and finally, construct the new filename.

The "tJava" component mentionned in the previous answer is the right place to do that.

Based on the suggested operations, the solution is as simple as:

 

String[]tokens = (String)globalMap.get("tFileList_1_CURRENT_FILE").split("_|\\.");
globalMap.put("NewFileName", tokens[0]+"_"+TalendDate.formatDate("yyyyMMdd", TalendDate.parseDateLocale("ddMMMyyyy", tokens[1], "EN"))+"."+tokens[2]);

It works like this:

  • the "String.split()" method separates each part from the "tFileList_1_CURRENT_FILE" global variable associated to the "tFileList_1" component, giving the "tokens" array composed of 3 strings ("xyz", "10JUL2018" and "csv" based on your example)
  • the result is stored into a new global variable I called "NewFileName", resulting from the concatenation of:
    • "tokens[0]" which corresponds to "xyz"
    • "_" which is the 1rst separator
    • the result of transformation of the date  format from "ddMMMyyyy" to "yyyyMMdd" using appropriate TalendDate routines
    • "." which is the 2nd separator
    • "token[2]" which corresponds to the file extension

 Notice: I prefer to use global variables instead of context variables as soon as I don't need to share them with other jobs, so you will have to change "Destination filename" parameter from the tFileCopy component using the following expression:

(String)globalMap.get("NewFileName")

Hope this helps.


TRF
Nine Stars

Re: Convert file name from xyz_10JUL2018.csv to xyz_20180710.csv

@TRF - has shown a very compact and wonderful way to obtain the needed date conversion process in tJava. 

Here goes mine which is bit long when compared and kind of self-explanatory as it's broken in step wise manner.

 

String dDate = null; 
SimpleDateFormat sdf=new SimpleDateFormat("ddMMMyyyy");   
String s =  ((String)globalMap.get("tFileList_1_CURRENT_FILE"));
String prefile = null; 

prefile = s.substring(0, s.indexOf("_"));
s = s.substring(s.indexOf("_") + 1);
s = s.substring(0, s.indexOf("."));
Date date=sdf.parse(s);
sdf=new SimpleDateFormat("yyyyMMdd");

dDate = sdf.format(date);
dDate = prefile + "_" + dDate+".csv";
context.newFileName = dDate;

Note: this code base required two libraries to be imported

import java.text.SimpleDateFormat;  
import java.util.Date;

All Replies
Nine Stars

Re: Convert file name from xyz_10JUL2018.csv to xyz_20180710.csv

One way of doing it will be tFileList -> iterate -> tJava -> iterate -> tFileCopy

Now, in tFileList you would traverse through all your files and tJava would iterate over all the files extract the name convert the date format as per your requirement and store it in an interim context variable. tFileCopy would be using that context variable value as the "Destination FileName" as you would be choosing to "Rename" the files.

Job Layout - 

tFileCopy-2.JPG

Forteen Stars TRF
Forteen Stars

Re: Convert file name from xyz_10JUL2018.csv to xyz_20180710.csv

@iamabhishek gave you the right direction regarding the general job design, but nothing about the date conversion which finally seems to be the main part of the question.

I assume the filename pattern is always the same:

  • some characters terminated by an underscrore "_"
  • the date formatted as ddMMMyyyy
  • the file extension introduced by a dot "."

A simple solution is to use the "String.split()" method to extract each part from the filename, then change the date format and finally, construct the new filename.

The "tJava" component mentionned in the previous answer is the right place to do that.

Based on the suggested operations, the solution is as simple as:

 

String[]tokens = (String)globalMap.get("tFileList_1_CURRENT_FILE").split("_|\\.");
globalMap.put("NewFileName", tokens[0]+"_"+TalendDate.formatDate("yyyyMMdd", TalendDate.parseDateLocale("ddMMMyyyy", tokens[1], "EN"))+"."+tokens[2]);

It works like this:

  • the "String.split()" method separates each part from the "tFileList_1_CURRENT_FILE" global variable associated to the "tFileList_1" component, giving the "tokens" array composed of 3 strings ("xyz", "10JUL2018" and "csv" based on your example)
  • the result is stored into a new global variable I called "NewFileName", resulting from the concatenation of:
    • "tokens[0]" which corresponds to "xyz"
    • "_" which is the 1rst separator
    • the result of transformation of the date  format from "ddMMMyyyy" to "yyyyMMdd" using appropriate TalendDate routines
    • "." which is the 2nd separator
    • "token[2]" which corresponds to the file extension

 Notice: I prefer to use global variables instead of context variables as soon as I don't need to share them with other jobs, so you will have to change "Destination filename" parameter from the tFileCopy component using the following expression:

(String)globalMap.get("NewFileName")

Hope this helps.


TRF
Nine Stars

Re: Convert file name from xyz_10JUL2018.csv to xyz_20180710.csv

@TRF - has shown a very compact and wonderful way to obtain the needed date conversion process in tJava. 

Here goes mine which is bit long when compared and kind of self-explanatory as it's broken in step wise manner.

 

String dDate = null; 
SimpleDateFormat sdf=new SimpleDateFormat("ddMMMyyyy");   
String s =  ((String)globalMap.get("tFileList_1_CURRENT_FILE"));
String prefile = null; 

prefile = s.substring(0, s.indexOf("_"));
s = s.substring(s.indexOf("_") + 1);
s = s.substring(0, s.indexOf("."));
Date date=sdf.parse(s);
sdf=new SimpleDateFormat("yyyyMMdd");

dDate = sdf.format(date);
dDate = prefile + "_" + dDate+".csv";
context.newFileName = dDate;

Note: this code base required two libraries to be imported

import java.text.SimpleDateFormat;  
import java.util.Date;