cancel
Showing results for 
Search instead for 
Did you mean: 

Need help with UDF for Dynamic File Naming

0 Kudos

Hi Team,

I have files in the source directory with the names as :

ATOSS_IMP_ZEITARTEN_20130712_141301.txt.zip

B1234561.TXT_20151208-125416-787.zip

PSA-201512.TXT.zip

BAV-Doppelversorgung_201512.xls.zip



I need to pick the files and remove the extension.zip from all the files and also the timestamp from files which have them using Dynamic Configuration in UDF.Timestamp will always be after _ and before the actual extension of the filename as highlighted above.

Please help me with the code for the same.


Required O/p for above files is :


ATOSS_IMP_ZEITARTEN.txt

B1234561.TXT

PSA-201512.TXT

BAV-Doppelversorgung.xls

Accepted Solutions (1)

Accepted Solutions (1)

0 Kudos

PFB the working code for no extensions,2 character extensions and 4 character extensions.:

public class testStr {

    public static void main(String[] args)

   

    {

       

      String a="";    

      String destFileName=null;

      String fileNameWithoutZip= a.substring(0,a.lastIndexOf("."));// no second . then o/p is -1 (-1+1=0)

        

      int dot=fileNameWithoutZip.lastIndexOf(".")+1;

      int length=fileNameWithoutZip.length();

     

      if (dot==0)

      {

     destFileName= fileNameWithoutZip.substring(0,length);

          System.out.println("no extension");

      }

      else if((length-dot)==3)

        {

             destFileName=fileNameWithoutZip;

             System.out.println("if1");

        

        }

        

        else

        {

        

             if ((length-dot)==4)

             {

            int dotNew=dot+4;             

                 destFileName= fileNameWithoutZip.substring(0,dotNew);

                 System.out.println("if2");

             }

             else

            {

            if ((length-dot)==24)

            { 

            int dotNew=dot+4;             

            destFileName= fileNameWithoutZip.substring(0,dotNew);

            System.out.println("if3");

            }

            else

            {

            int dotNew=dot+3;             

            destFileName= fileNameWithoutZip.substring(0,dotNew);

            System.out.println("else");

            }

                         

            }

        }

               

System.out.println(destFileName);

  }

 

}

Answers (4)

Answers (4)

0 Kudos

The updated requirement is :

Original File Names

Required File Name

ATOSS_IMP_ZEITARTEN_20130712_141301.txt.zip

ATOSS_IMP_ZEITARTEN_20130712_141301.txt

B1234561.TXT_20151208-125416-787.zip

B1234561.TXT

PSA-201512.TXT.zip

PSA-201512.TXT

BAV-Doppelversorgung_201512.xls.zip

BAV-Doppelversorgung_201512.xls

After .txt/.xml/.csv or any other extension we will have the timestamp as highlighted above.

First we have to remove .zip

Then check for the timestamp,if present then remove the same or else do nothing.

Please share your inputs for the same.


Thanks

Rajat

vicky20691
Active Contributor
0 Kudos

Hi again,

lets see if the below works

DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");

String sourceFileName = conf.get(key);

String destFileName=null;

String fileNameWithoutZip= sourceFileName.substring(0,sourceFilleName.lastIndexOf("."));

int dot=fileNameWithoutZip.lastIndexOf(".");

int length=fileNameWithoutZip.length();

if((length-dot)==3)

{

     destFileName=fileNameWithoutZip;

}

else

{

     int dotNew=dot+3;

     destFileName= fileNameWithoutZip.substring(0,dotNew);

}

DynamicConfigurationKey key1= DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");

key1.put(destFileName);

don't put smiley in code !

Regards,

Vikas

0 Kudos

Resolved the error by using this :

DynamicConfigurationKey key1= DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");

conf.put(key1,destFileName);

return "";

But not getting what is required.

Plus we can have any extension file name like .idif or anything.

May be we have to look at using regex.

0 Kudos

Hi Vikas,

Thank you for the help.

I have used the below code with no input to UDF:

DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");

String sourceFileName = conf.get(key);

String destFileName=null;

String fileNameWithoutZip= sourceFileName.substring(0,sourceFileName.lastIndexOf("."));

int dot=fileNameWithoutZip.lastIndexOf(".");

int length=fileNameWithoutZip.length();

if((length-dot)==3)

{

     destFileName=fileNameWithoutZip;

}

else

{

     int dotNew=dot+3;

     destFileName= fileNameWithoutZip.substring(0,dotNew);

}

DynamicConfigurationKey key1= DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");

conf.put(key1,destFileName);

return "";

Error:

Runtime exception when processing target-field mapping /ns0:MT_Source; root message: Exception:[java.lang.StringIndexOutOfBoundsException: String index out of range: -1] in class com.sap.xi.tf._

0 Kudos

Hi Vikas,

The below code works for all files names that have extension as 3 characters like .csv,.txt.xls.dat:

public class testStr {

    public static void main(String[] args)

   

    {

        String destFileName=null;

        String fileNameWithoutZip= a.substring(0,a.lastIndexOf("."));       

        int dot=fileNameWithoutZip.lastIndexOf(".")+1 ;        

        int length=fileNameWithoutZip.length();

        

        if((length-dot)==3)

        

        {

             destFileName=fileNameWithoutZip;

             System.out.println("if");

        

        }

else

        {

        

             int dotNew=dot+3;

             destFileName= fileNameWithoutZip.substring(0,dotNew);

             System.out.println("else");

                         

        }

                

System.out.println(destFileName);

  }

Tried examples and worked:

1.ATOSS_IMP_ZEITARTEN_20130712_141301.txt.zip

2.B1234561.TXT_20151208-125416-787.zip

3.PSA-201512.TXT.zip

4.BAV-Doppelversorgung_201512.xls.zip

Can you suggest the changes in the code for file extensions of 4 characters like ".idif" ".jpeg"

Sample filenames being:

1. SAP_PEOPLE_TDG_ORG.ldif_20160112-153106-297.zip   expected : SAP_PEOPLE_TDG_ORG.ldif

2. SAP_PEOPLE_TDG_ORG_20160112-153106-297.ldif.zip   expected :SAP_PEOPLE_TDG_ORG_20160112-153106-297.ldif

0 Kudos

Some changes in the requirement will post the updated one in sometime.

I am trying to find a pattern to write a UDF and will require help for the same.

Thanks

Rajat

vicky20691
Active Contributor
0 Kudos

Hi rajat,

try this

DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);

DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");

String sourceFileName = conf.get(key);

String fileNameWithoutDate= sourceFileName.substring(0,sourceFilleName.lastIndexOf("_"));

String destFileName=fileNameWithourDate+".txt";

DynamicConfigurationKey key1= DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");

key1.put(destFileName);


Regards,

Vikas

0 Kudos

Hi Vikas,

The file extension can be .xls as well or any other extension.

We cannot be sure of adding .txt to every file name as per the UDF.

Regards

Rajat

vicky20691
Active Contributor
0 Kudos

Hi rajat,

I added it as an example. So how will you determine the extension, afater removing the zip, you want to add which extension?

Regards,

Vikas

former_member186851
Active Contributor
0 Kudos

Guess the better way would be to remove the timestamp from the previous interface.

vishnu_pallamreddy
Contributor
0 Kudos

Hi Rajat,

Please check below thread, u will get some knowledge

0 Kudos

File names are in random pattern so difficult to parse.

Although we have the liberty of using Message mapping but looks difficult to write a UDF for the same as _ can be anywhere in the file name and file actual file extension like .txt/.xls can be before or after the timestamp in the file name.

former_member186851
Active Contributor
0 Kudos

Rajat,

Before handling this In PI,Can you request them to place the file without timestamp and .ZIP extension.

former_member186851
Active Contributor
0 Kudos

.ZIP can be removed by using replaceall Function,Removing timestamp is the complicated thing.

0 Kudos

This cannot be done as files are getting written in Archive Folder by some other interface.

former_member186851
Active Contributor
0 Kudos

Rajat,

Then go to interface find the channel ,Remove the append timestamp in that channel.