Using mtime from tFileProperties to rename a file

Highlighted
Six Stars

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?

 


Accepted Solutions
Employee

Re: Using mtime from tFileProperties to rename a file

Hi,

 

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

image.png

 

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

image.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


All Replies
Employee

Re: Using mtime from tFileProperties to rename a file

Hi,

 

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

image.png

 

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

image.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

Six Stars

Re: Using mtime from tFileProperties to rename a file

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

2019 GARTNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Best Practices for Using Context Variables with Talend – Part 3

Read about some useful Context Variable ideas

Blog