Skip to main content
Announcements
Introducing a new Enhanced File Management feature in Qlik Cloud! GET THE DETAILS!
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Using mtime from tFileProperties to rename a file

I have a job which runs daily against some source files, with the ultimate output being a CSV file.  Each day, when the job runs, it renames the CSV, and puts the current date into the filename using this bit of code:

 

"Filename_"+TalendDate.formatDate("ddMMyyyy",TalendDate.getCurrentDate())+".csv"

However, what I would like to do instead, is use tFileProperties to get the modified time of the file (mtime), and convert that into the string to use in the filename instead.  This is where I am struggling.

 

So I load the file into the tFileProperties component

 

I then grab the mtime value and created a variable called "stringdate" in my tMap to convert it to ddmmyyyy using this code:

 

new java.util.Date((long)(row1.mtime*1000)/1000)

Outputted on its own, if the date was 27th August 2019, this then outputs 27082019 as the result, which is exactly what I want.  However, this is in date format, so to add it to my filename it needs to be converted into a simple 8 digit string.

 

In my tFileCopy where I create the copy of the file and rename it, I use this code.

 

"Filename_" +  Var.stringdate + ".csv"

However the result isn't "Filename_27082019.csv", instead it is "Filename_Tue Aug 27 09:09:46 BST 2019.csv"

 

How do I get around this?

 

Labels (2)
1 Solution

Accepted Solutions
Anonymous
Not applicable
Author

Hi,

 

     The most easy way is to use mtime_string value to do the operation. Please refer the screenshots below for a sample file.

0683p000009M7Ep.png

 

You can add the transformations in the tMap as shown below.

0683p000009M7DJ.png

 

date_string -> TalendDate.formatDate("ddMMyyyy",TalendDate.parseDate("EEE MMM dd HH:mm:ss zzz yyyy",row1.mtime_string)) 

filename_bfr_extn -> row1.basename.substring(0, row1.basename.indexOf(".")) 

filename_aft_extn -> row1.basename.substring(row1.basename.indexOf(".")+1,row1.basename.length() ) 

output_file_name -> Var.filename_bfr_extn +"_"+Var.date_string  +"."+Var.filename_aft_extn 

I hope I have answered your query. Please spare a second to mark the topic as resolved. Kudos will be bonus!

 

Warm Regards,
Nikhil Thampi

Please appreciate our Talend community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved

 

View solution in original post

3 Replies
Anonymous
Not applicable
Author

Hi,

 

     The most easy way is to use mtime_string value to do the operation. Please refer the screenshots below for a sample file.

0683p000009M7Ep.png

 

You can add the transformations in the tMap as shown below.

0683p000009M7DJ.png

 

date_string -> TalendDate.formatDate("ddMMyyyy",TalendDate.parseDate("EEE MMM dd HH:mm:ss zzz yyyy",row1.mtime_string)) 

filename_bfr_extn -> row1.basename.substring(0, row1.basename.indexOf(".")) 

filename_aft_extn -> row1.basename.substring(row1.basename.indexOf(".")+1,row1.basename.length() ) 

output_file_name -> Var.filename_bfr_extn +"_"+Var.date_string  +"."+Var.filename_aft_extn 

I hope I have answered your query. Please spare a second to mark the topic as resolved. Kudos will be bonus!

 

Warm Regards,
Nikhil Thampi

Please appreciate our Talend community members by giving Kudos for sharing their time for your query. If your query is answered, please mark the topic as resolved

 

Anonymous
Not applicable
Author

That's perfect, thank you. Using this I've managed to incorporate it into my job and it works fine. Many thanks
lec86042
Contributor
Contributor

I know this is an old post but I am trying to do something similar.

 

How did you get to use the tMap variable in your tFileCopy component?