Copy multiple files and rename them and remove the source file

Nine Stars

Copy multiple files and rename them and remove the source file

I am trying to iterate through multiple files all present in a single directory, get each of their filenames change the name accordingly and once the rename & copy operation is a success would like to remove the original file.
The current process is able rename all the files as per the criteria but when the option is chosen to "Remove Source File" the process is getting error-ed out.

  1. tFileList is used to get the list of all the files in the directory.
  2. tIterate is used to iterate over the list of files.
  3. Used tMap to derive the new file name for each of them - 
  4. Using tContextLoad to load the context variable with the derived file name
  5. And finally in the tFileCopy providing the required details. 

The process will be copying the new files in the same directory as the source file is present.

Expression used in tMap to derive the filename - 

row1.fileName.substring(0,StringHandling.INDEX(row1.fileName,"."))+"_"+TalendDate.formatDate("MMddyyyy",TalendDate.getCurrentDate())+row1.fileName.substring(StringHandling.INDEX(row1.fileName,"."),row1.fileName.length())

Job screenshot -

tFileCopy-1.JPG

Am I following the correct approach in here or is there something that needs to be changed. Thanks in advance.

Platform: Talend Open Studio for Data Integration Version: 6.5.1


Accepted Solutions
Forteen Stars TRF
Forteen Stars

Re: Copy multiple files and rename them and remove the source file

tFileList-->tFileCopy should be enough.
Try this expression for the destination filename:
((String)globalMap.get(tFileList_1_CURRENT_FILE)).replace(((String)globalMap.get(tFileList_1_FILEEXTENSION)), "_" + TalendDate.formatDate("MMddyyyy", TalendDate.getCurrentDate()) + ((String)globalMap.get(tFileList_1_FILEEXTENSION))

Tick the options "Rename" and "Remove source file".
Hope this help.

TRF
Forteen Stars TRF
Forteen Stars

Re: Copy multiple files and rename them and remove the source file

Thank's for the feddback and sorry for the mistake, I answered from my mobile...

Here is the correct syntax:

((String)globalMap.get("tFileList_1_CURRENT_FILE"))
    .replace("." + ((String)globalMap.get("tFileList_1_CURRENT_FILEEXTENSION")), 
               "_" + TalendDate.formatDate("MMddyyyy", TalendDate.getCurrentDate()) + 
"." + ((String)globalMap.get("tFileList_1_CURRENT_FILEEXTENSION")))

 


TRF

All Replies
Forteen Stars TRF
Forteen Stars

Re: Copy multiple files and rename them and remove the source file

tFileList-->tFileCopy should be enough.
Try this expression for the destination filename:
((String)globalMap.get(tFileList_1_CURRENT_FILE)).replace(((String)globalMap.get(tFileList_1_FILEEXTENSION)), "_" + TalendDate.formatDate("MMddyyyy", TalendDate.getCurrentDate()) + ((String)globalMap.get(tFileList_1_FILEEXTENSION))

Tick the options "Rename" and "Remove source file".
Hope this help.

TRF
Nine Stars

Re: Copy multiple files and rename them and remove the source file

@TRF Thanks a ton for the suggestion. I definitely went overboard with my approach.

The expression suggested by you was having a minute problem with the extension so just made a little tweak to the expression for the destination file name -

((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(0,StringHandling.INDEX(((String)globalMap.get("tFileList_1_CURRENT_FILE")),"."))+"_"+TalendDate.formatDate("MMddyyyy",TalendDate.getCurrentDate())+((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(StringHandling.INDEX(((String)globalMap.get("tFileList_1_CURRENT_FILE")),"."),((String)globalMap.get("tFileList_1_CURRENT_FILE")).length())
Forteen Stars TRF
Forteen Stars

Re: Copy multiple files and rename them and remove the source file

Thank's for the feddback and sorry for the mistake, I answered from my mobile...

Here is the correct syntax:

((String)globalMap.get("tFileList_1_CURRENT_FILE"))
    .replace("." + ((String)globalMap.get("tFileList_1_CURRENT_FILEEXTENSION")), 
               "_" + TalendDate.formatDate("MMddyyyy", TalendDate.getCurrentDate()) + 
"." + ((String)globalMap.get("tFileList_1_CURRENT_FILEEXTENSION")))

 


TRF
Nine Stars

Re: Copy multiple files and rename them and remove the source file

No problem @TRF.

This one works like a charm Smiley Happy