Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Toronto Sept 9th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

[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.
Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

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 0683p000009MPcz.png

View solution in original post

11 Replies
Anonymous
Not applicable
Author

attaching file again
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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 0683p000009MPcz.png
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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.
Anonymous
Not applicable
Author

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?
Anonymous
Not applicable
Author

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")) 
Anonymous
Not applicable
Author

thank you very much .. it looks complete now .. Got It 0683p000009MACn.png
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 0683p000009MACn.png