How to change file name / extension for number of files?

Hello community Smiley Happy
I need your advice for the following problem:
I have a job that gets files from remote FTP server.
I need to archive these files and then change file names / extensions for originally downloaded files. (see job print screens)
Naming of source files is done according to the following mask: cccccc_yyyy-MM-dd.dat
I need to keep only first 6 characters and .dat should be changed to .csv
My idea was to use tFileCopy with Rename to change file name/extension. But it fails...
Looks like I can't use Talend StringHandling system routine to build destination filename
Like LEFT(((String)globalMap.get("tFileList_1_CURRENT_FILE")),6)+".csv"
The warning/error is:
Warning! Some errors exist in jobs.
The method LEFT(string, int) is undefined for the type get_files_from_ftp
Any idea how can I change file name / extension for number of downloaded files?
6 REPLIES
Community Manager

Re: How to change file name / extension for number of files?

Hello
The destination name should be:
StringHandling.LEFT(((String)globalMap.get("tFileList_1_CURRENT_FILE")),6)+".csv"

Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business

Re: How to change file name / extension for number of files?

Thank you Shong. Now it works...
One Star

Re: How to change file name / extension for number of files?

hi shong!!!

Ur solution seem to solve my problem..
but i need to add the counting number in between in file name..
for example
StringHandling.LEFT(((String)globalMap.get("tFileList_1_CURRENT_FILEPATH")),6)+ ".txt"
this returns as 'sample.txt'
but i want it as 'sample_001.txt'
001 should be the file count that retrieves per day..
Thank u so much!!!!
Five Stars

Re: How to change file name / extension for number of files?

Can someone please help out with the solution to this last issue/problem.

 

Thanks

Employee

Re: How to change file name / extension for number of files?

Hi,

 

tFileList_1_NB_FILE parameter can give the file count under a folder. You can store the details in a context variable if needed for later use such as appending the number to file extension.

 

Warm Regards,

 

Nikhil Thampi

 

Tags (1)
Forteen Stars TRF
Forteen Stars

Re: How to change file name / extension for number of files?

If your job run on a daily basis, you can use the following:
StringHandling.LEFT(((String)globalMap.get("tFileList_1_CURRENT_FILEPATH")),6) + "_" + StringHandling.RIGHT("000"+(Integer)globalMap.get("tFileList_1_NB_FILE"),3) + ".txt"

If it run more than once per day, you need to associate a counter to a context variable which will be retrieve from a properties file each time the job will start. Then you will have to change the expression into the tFileCopy like this:
StringHandling.LEFT(((String)globalMap.get("tFileList_1_CURRENT_FILEPATH")),6) + "_" + StringHandling.RIGHT("000"+((Integer)globalMap.get("tFileList_1_NB_FILE")+context.Counter),3) + ".txt"

Have a look at tContextLoad / tContexDump component to know how to set a context variable from a file (with help of tFileInputDelimited) and how to store it into a file (with help of tFileOutputDelimited).
You will need also an other context variable to count the job runs and decide when to reset the all the counters to 1 (for example, if the job runs every hour, reset should occur when the run counter reach the value of 25).
Hope this helps.

TRF