cancel
Showing results for 
Search instead for 
Did you mean: 

How to rename the Target filename.

Former Member
0 Kudos

Hi...

I n file to file sceanrio, i need to rename the existing target file name...

I have searched in SDN. i have found like we can achive it by batch program.

can you please help how to do this...?

Regards,

Leela

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Give some proper example, becasue target file name can be set as per you wish in Receiver File Adapter. Else if you need to some other things in you target file name then you have to use Dynamic Configuration in your message mapping. If this is the case then search on SDN, ther is a blog written by Michal on Dynamic configuration that will help you..

Regards,

Sarvesh

Former Member
0 Kudos

HI..

In Input file iwe are having location field. We need to chjeck the target directory and filename is present or not.Then we need to rename the target filename with the loctaion.

Can you please help in writing the udf for the above requirement.

Regards,

Leela

former_member187339
Active Contributor
0 Kudos

Hi Leela,

Try this udf

import java.io.File in the udf code

old_name = old filename

dir = directory

new_name = new filename


String old_file = dir + old_name
File oldfile = new File(file_name);
if(!oldfile.exists()) {
     System.exit(0);
}
String new_file = dir + new_name;
File newfile = new File(new_name);
boolean Rename = oldfile.renameTo(newfile);
if(!Rename) {
        System.exit(0);
}

Regards

Suraj

Former Member
0 Kudos

HI..

In Input file iwe are having location field. We need to chjeck the target directory and filename is present or not.Then we need to rename the target filename with the loctaion.

Can you please help in writing the udf for the above requirement.

Regards,

Leela

Former Member
0 Kudos

> We need to chjeck the target directory and filename is present or not.Then we need to rename the target filename with the loctaion.

Do you mean there will be always only one file at any time?

You have to write the UDF for Dynamic Configuration in order to rename the file with your location.

UDF Code

You have to pass you location name to this UDF in your mapping.

DynamicConfiguration conf = (DynamicConfiguration) container.getTransformationParameters()
.get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
 
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
 
String SourceFileName =  location;
 
conf.put(key, SourceFileName);
 
return "";

Note: You have to set the ASMA settings under Advanced Tab in both the adapters.

Now you second requirement is to first check if target directory and filename is present or not.

So this UDF has no control to check if file is present or not. The UDF will always write the filename with location.

Probably to perform this check before writing the file name you can try with

"Run operationg system command before message processing", otherwise you may have to write an adapter module for this.

Regards,

Sarvesh

Former Member
0 Kudos

Hi...

Tto perform the check before writing the file name is there is no other option with UDF ? other than "Run operationg system command before message processing", or an adapter module for this.

Regards,

Leela

Former Member
0 Kudos

> Tto perform the check before writing the file name is there is no other option with UDF ? other than "Run operationg system command before message processing", or an adapter module for this.

Well my ans for above ques is "NO". May be somebody from the forum can suggest you some other solution.

If you explain the whole mechanism step by step then also we may find some other alternate solution.

former_member187339
Active Contributor
0 Kudos

Hi Leela,

Did you tried the code which I have mentioned earlier. It first check and if a file is found it renames it..

You can use it as a udf

Regards

Suraj

Former Member
0 Kudos

Hi Suraj,

I have tried the code which you have provided. But it showing the below errors.

Function fileRename, Line 1:

operator + cannot be applied to java.lang.String[],java.lang.String[] String old_file = dir + old_name; ^

Function fileRename, Line 2:

cannot find symbol symbol : variable file_name location: class com.sap.xi.tf._MM_FILETOFILE1_ File oldfile = new File(file_name);

Regards,

Leela

former_member187339
Active Contributor
0 Kudos

Hi Leela,

>>operator + cannot be applied to java.lang.Stringhttp://],java.lang.String[ String old_file = dir + old_name; ^

Hope you have three input to the UDF and all are of type string and not string array... also the udf is a simple udf.


String old_file = dir + old_name;
File oldfile = new File(old_file);
if(!oldfile.exists()) {
     System.exit(0);
}
String new_file = dir + new_name;
File newfile = new File(new_name);
boolean Rename = oldfile.renameTo(newfile);
if(!Rename) {
        System.exit(0);
}
return;

PS: Made few adjustment in the code. Use this and check

Regards

Suraj

Former Member
0 Kudos

HIi.. Suraj,

After replacing with the new code, i am getting the below error.

Function fileRename, Line 1:

operator + cannot be applied to java.lang.String[],java.lang.String[] String old_file = dir + old_name; ^

Function fileRename, Line 6:

operator + cannot be applied to java.lang.String[],java.lang.String[] String new_file = dir + new_name; ^

Function fileRename, Line 7:

cannot find symbol symbol : constructor File(java.lang.String[]) location: class java.io.File File newfile = new File(new_name);

Regards,

Leela

Former Member
0 Kudos

Hi Suraj...

Thanks for your code.... I am using Advanced UDF. I have changed to simple udf. Now it is not showing any errors.

While Testing... it is showing the below error..in moni.

<SAP:Code area="MAPPING">JCO_SYSTEM_FAILURE</SAP:Code>

<SAP:P1>connection to partner 'loopback:0' broken / CPIC-CALL: 'ThSAPCMRCV' : cmRc=20 th</SAP:P1>

Regards,

Leela

Former Member
0 Kudos

The renameTo method used in this UDF works only for a local file system. When the renameTo is used to rename a file

on the local file system to a NFS mounted path it fails.

Regards,

Sarvesh

former_member187339
Active Contributor
0 Kudos

Hi Leela,

Are you using NFS or FTP.. And one more thing when you concatenate dir and filename, you need to put a / between them, are you doing that too? (i forgot to mention it in my code)

So the old filename will be dir/old_file and same is for new file name

Regards

Suraj