Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us in Bucharest on Sept 18th for Qlik's AI Reality Tour! Register Now
cancel
Showing results for 
Search instead for 
Did you mean: 
iamabhishek
Creator III
Creator III

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 -

0683p000009LxTC.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

Labels (2)
1 Solution

Accepted Solutions
TRF
Champion II
Champion II

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.

View solution in original post

5 Replies
TRF
Champion II
Champion II

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.
iamabhishek
Creator III
Creator III
Author

@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())
TRF
Champion II
Champion II

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")))

 

iamabhishek
Creator III
Creator III
Author

No problem @TRF.

This one works like a charm 0683p000009MACn.png

Anonymous
Not applicable

Hi 

 

 have multiple folder structures with same file name in each folders , when I am using tFileCopy it is taking only loading unique file name to the destination and excluding the rest. I would like to rename the the file with its sub folder name associated with it so that each file will have unique name , Can you please guide ,how will execute this .

  

     * Renaming with date and extension is not working in this case . 

   


Filecopy.PNG