Dynamic FileName Change

Highlighted
Six Stars

Dynamic FileName Change

Hi Team

 

I have a simple Scenario

Using tFileList  i am reading a list of files and converting them to excel formats

while doing this process i want target file name to same as source file name but with Excel extension

How can this be achived,

I am using Using "((String)globalMap.get("tFileList_1"))" to get the filname,but i am getting file name along with source extension.

I am attaching the screenshot here with

 

Please help me out how this can be done

 

Thanks in advance

Deepthi

Deepthi


Accepted Solutions
Community Manager

Re: Dynamic FileName Change

You just need to use a tiny bit of Java. It is probably better to turn this into a routine so that your code doesn't look messy in your job, but this is how you would do without a routine....

 

((String)globalMap.get("tFileList_1")).substring(0,((String)globalMap.get("tFileList_1")).indexOf('.'))+".xls"

 

As a routine it might look like this......

public static String replaceFileExtension(String filename, String extension){
     String returnVal = filename;
     
    if(filename!=null && filename.indexOf('.')>-1){
           returnVal = filename.substring(0, filename.indexOf('.'))+"."+extension;
    }
    return returnVal;
}

You would use the above like below....

 

replaceFileExtension(((String)globalMap.get("tFileList_1")), "xls")

 

#I should point out that I have written this freehand so there may be a couple of tweaks required.


All Replies
Community Manager

Re: Dynamic FileName Change

You just need to use a tiny bit of Java. It is probably better to turn this into a routine so that your code doesn't look messy in your job, but this is how you would do without a routine....

 

((String)globalMap.get("tFileList_1")).substring(0,((String)globalMap.get("tFileList_1")).indexOf('.'))+".xls"

 

As a routine it might look like this......

public static String replaceFileExtension(String filename, String extension){
     String returnVal = filename;
     
    if(filename!=null && filename.indexOf('.')>-1){
           returnVal = filename.substring(0, filename.indexOf('.'))+"."+extension;
    }
    return returnVal;
}

You would use the above like below....

 

replaceFileExtension(((String)globalMap.get("tFileList_1")), "xls")

 

#I should point out that I have written this freehand so there may be a couple of tweaks required.

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

Definitive Guide to Data Integration

Practical steps to developing your data integration strategy.

Download