[resolved] get date from file name

Hi,
I am unable to get Date from a file name to use in tmap expression editor.
Attached is my Job design and tMap editor view. 
1) The tFileRowCount input file name is like "Data_f.cat.fy16.detail.201602.txt". Now I need to read the date part which is "201602" in this case (YYYYMM) and pass it to the "ExtractDate" expression in the tMap expression editor on the output file. For now I've hardcoded "ExtractDate=201602" as you can see in the attached image, but as per the requirement it should read the date from the actual date file name, which is given as input for tFileRowCount in the current job.
2) Is there any way to achieve with and/or without tJava related components
Let me know if I am not very clear about the requirement.
thank you for your time.
1 ACCEPTED SOLUTION

Accepted Solutions

Re: [resolved] get date from file name

Hi, thank you for quick reply.
I've already managed to get the file name using tFileList. But what I could not figure out was how to get only date (201602) from "Data_f.cat.fy16.detail.201602.txt" and use it in tMap Smiley Sad
11 REPLIES

Re: [resolved] get date from file name

attaching file again
Fifteen Stars

Re: [resolved] get date from file name

Since you do not know the filename, I am assuming that you are using the tFileList component to pass the filename to the component you are using to read it. If you aren't, then you probably should be. If you tFileList is called tFileList_1, the way to get the filename is to use this code....
((String)globalMap.get("tFileList_1_CURRENT_FILE"))

You can use that in a tMap and use a regex to get the date from the filename.
Rilhia Solutions

Re: [resolved] get date from file name

Hi, thank you for quick reply.
I've already managed to get the file name using tFileList. But what I could not figure out was how to get only date (201602) from "Data_f.cat.fy16.detail.201602.txt" and use it in tMap Smiley Sad
Fifteen Stars

Re: [resolved] get date from file name

You can use Java String functions "substring" and "indexOf" to do this. You know that the date comes just before ".txt" and you know it is 6 characters. That is all you need.
Rilhia Solutions

Re: [resolved] get date from file name

Hi ..
I have used the java string function as below, to get the date from the file name "Data_f.cat.fy16.detail.201602.txt" :
"ExtractDate="+(((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(23,((String)globalMap.get("tFileList_1_CURRENT_FILE")).indexOf(".txt")))
I've used 23 as that is the number of characters before 201602 in the file name, and then I got the output as desired which is ExtractDate=201602.
but, what if the number of characters before the date in the filename changes or varies?
PS: Sorry, I'm not from core development / programming background.
Fifteen Stars

Re: [resolved] get date from file name

You use "indexOf" to work out where the ".txt" is in the String. Then you know that the start of your date is 6 characters before that position. You then have your beginning and end position.
Rilhia Solutions

Re: [resolved] get date from file name

I might sound too dumb but ..
what do I pass instead of 23? how do I tell to get last 6 digits (before .txt) instead of telling to ignore first 23 characters?
Fifteen Stars

Re: [resolved] get date from file name

Try this.....
((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(((String)globalMap.get("tFileList_1_CURRENT_FILE")).indexOf(".txt")-6,((String)globalMap.get("tFileList_1_CURRENT_FILE")).indexOf(".txt")) 
Rilhia Solutions

Re: [resolved] get date from file name

thank you very much .. it looks complete now .. Got It Smiley Happy
where can I learn or what key words shall I use to search google .. to learn similar java (or string or any other) related functions to use in Talend .. not to learn complete Java.
thanks again Smiley Happy
Fifteen Stars

Re: [resolved] get date from file name

Learning Java is a must for Talend when you want to see what Talend can really do. But even the smallest things make use of simple Java concepts. Look for some Java tutorials online and download a copy of Eclipse (or similar). Once you have got a few examples under your belt, you will start to understand a bit more to ask (Google) the right questions. A lot of people (at University and other places) use a book called "Objects First with BlueJ" (http://www.bluej.org/objects-first/).
Rilhia Solutions

Re: [resolved] get date from file name

thank you .. i'll start learning Java for sure.