Four Stars

How to rename a file output based on a variable?

I need to (1) make a copy of a file and (2) rename the copied file based on a variable that resides within the file.   I have used the tFileCopy component to copy the file, and also managed to extract the variable from the file e.g. date within the file(report), but I am not sure how I could insert this extracted variable (date) into the tFileCopy component's Destination Filename field.

 

Desperately need guidance to this, thank you

1 ACCEPTED SOLUTION

Accepted Solutions
Employee

Re: How to rename a file output based on a variable?

If you have successfully extracted the value from the file you need to add to the file name then simply use the tContextLoad to load that value into a predefined context variable.  You can use a tmap with 2 columns to set the context load it needs one column named "key" which should be your predefined context and the second column name is "value" and that equals the value from the file.

 

So at a high level steps

  1. Create Predefined Context named "filenameCTX"
  2. Read file that has value you need and connect that output to a tMap
  3. In tMap have 2 columns (key & value)
  4. Assign key = "filenameCTX" 
  5. Assign value = (value from inside file)
  6. Attach tMap Output to tContextLoad
  7. In tFileCopy component's Destination Filename field use context variable filenameCTX + ".csv"  (for example)

If you have multiple files to do this with you can put steps 2-7 in a subjob and use a tFileList to iterate over the the subjob. 

 

 

you can even put a date stamp on the file as well so do something like filenameCTX + TalendDate.formatDate("yyyy-MM-dd HH:mm:ss",TalendDate.getCurrentDate()) ".csv"  

 

If you need screen shots let me know.

3 REPLIES
Moderator

Re: How to rename a file output based on a variable?

Hello,

Could you please elaborate your case with an example with input and expected output values?

Best regards

Sabrina

--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Employee

Re: How to rename a file output based on a variable?

If you have successfully extracted the value from the file you need to add to the file name then simply use the tContextLoad to load that value into a predefined context variable.  You can use a tmap with 2 columns to set the context load it needs one column named "key" which should be your predefined context and the second column name is "value" and that equals the value from the file.

 

So at a high level steps

  1. Create Predefined Context named "filenameCTX"
  2. Read file that has value you need and connect that output to a tMap
  3. In tMap have 2 columns (key & value)
  4. Assign key = "filenameCTX" 
  5. Assign value = (value from inside file)
  6. Attach tMap Output to tContextLoad
  7. In tFileCopy component's Destination Filename field use context variable filenameCTX + ".csv"  (for example)

If you have multiple files to do this with you can put steps 2-7 in a subjob and use a tFileList to iterate over the the subjob. 

 

 

you can even put a date stamp on the file as well so do something like filenameCTX + TalendDate.formatDate("yyyy-MM-dd HH:mm:ss",TalendDate.getCurrentDate()) ".csv"  

 

If you need screen shots let me know.

Four Stars

Re: How to rename a file output based on a variable?

 Brilliant, thanks heaps for the solution.