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

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?
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)
Fifteen Stars TRF
Fifteen 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
Two Stars

Actually I want file as in the format PrimarySalesAggData_AnalysisTeam_YYYYMM.it should change dynamically depends on system date.if iam loading this data in tho amazon s3 using mssql in this month i should get file name as previous month .

 

Actually iam using file name as :-

"D:/Talend/CSV_Output/PrimarySalesAggData_AnalysisTeam_"+TalendDate.formatDate("yyyy-MM",TalendDate.getCurrentDate())+".csv" (it is working for me)

It is working up to this month if iam running this job toady i want the file name as previous month likePrimarySalesAggData_AnalysisTeam_201908

How to achieve this and while extracting to s3 iam giving key as

"PrimarySalesAggData_AnalysisTeam_"+TalendDate.formatDate("yyyy-MM",TalendDate.getCurrentDate())"

 but iam getting error as string literal is not properly close by double quotes.

can someone help me

 

2019 GARNER 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

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 4

Pick up some tips and tricks with Context Variables

Blog

How Media Organizations Achieved Success with Data Integration

Learn how media organizations have achieved success with Data Integration

Read

Agile Data lakes & Analytics

Accelerate your data lake projects with an agile approach

Watch