Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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
@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:
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:
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.
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 -
@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:
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:
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 - 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;