cancel
Showing results for 
Search instead for 
Did you mean: 

File name at receiver side, Mapping fields as one string

saravanan_a
Active Participant
0 Kudos

Hi Experts,

I have 2 requirements.

My scenario is file to file without ESR.

1) My requirement is, I need file name at receiver side as file name+date and not file name+time stamp.

2) If I am using ESR, and I need to make all the fields at sender side as one string and map it with target side also as one string and not field by field mapping.

  Suggest me how to achieve this?

Accepted Solutions (0)

Answers (6)

Answers (6)

Former Member
0 Kudos

Yeah,actually I have files on sender side in random order but i want that when i get these files on receiver side it will be in proper sequence.I hope u will understand my requirement

Former Member
0 Kudos

Hi Shruti, The sender file adapter (NFS) itself have standard functionality of sorting the files by name or date. If that doesnt fit in your requirement or you are using FTP protocol then next option would be write a OS command to sort files and use it in before message processing section of file adapter or write a custom adapter module to sort the files. Search for these option of SDN and you will find lots of content for it. Let me know if you need help with any of these.

- Amol

Former Member
Former Member
0 Kudos

You can create the ESR objects and then change the filename dynamically.

You can create data type as XSDstring base64 for sender and receiver.

Do a simple java mapping to move source contents to target contents and change filename....

Regards,

Lalit Mohan Gupta.    

Former Member
0 Kudos

Hi,

1) My requirement is, I need file name at receiver side as file name+date and not file name+time stamp.

  You can use simple Java mapping to rename file using dynamic configuration. I have used it in many scenarios. Here is the snippet of the code-

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.Reader;

import java.io.UnsupportedEncodingException;

import java.text.SimpleDateFormat;

import java.util.Date;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.DynamicConfiguration;

import com.sap.aii.mapping.api.DynamicConfigurationKey;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

public class RenameFile extends AbstractTransformation {

   

     private static final DynamicConfigurationKey KEY_FILENAME =

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

   

    @Override

    public void transform(TransformationInput in, TransformationOutput out)

            throws StreamTransformationException {

        // TODO Auto-generated method stub

       

        String newFileName = null;

       

        Date date = new Date(); // get the current date

        SimpleDateFormat dateFormatter = new SimpleDateFormat ("yyyyMMddHHmmSSS"); // set the format for date

        String dfmt =  dateFormatter.format(date);

        String timestamp = dfmt.substring(0, 14); // for yyyyMMddHHmmss

        String monthDay = dfmt.substring(4,8);  // for MMdd

        String mins = dfmt.substring(10,12);    // for mm

        String millisecs = dfmt.substring(12);  // for SSS

        String seqID = monthDay + mins + millisecs;     // the format being MMddmmSSS

       

       

        DynamicConfiguration conf = in.getDynamicConfiguration();

          String oldFileName = conf.get(KEY_FILENAME);               

         

          oldFileName = oldFileName.replaceAll(".txt", "");

     

          newFileName = oldFileName + seqID + ".txt." ; // the final filename

                   

          String inData = convertStreamToString(in.getInputPayload().getInputStream());

          String outData = inData;

          try {

            out.getOutputPayload().getOutputStream().write(outData.getBytes("UTF-8"));

        } catch (UnsupportedEncodingException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

          conf.put(KEY_FILENAME,newFileName);

    }

   

      public String convertStreamToString(InputStream in){

            StringBuffer sb = new StringBuffer();

            try

            {

            InputStreamReader isr = new InputStreamReader(in);

            Reader reader =

            new BufferedReader(isr);

            int ch;

            while((ch = in.read()) > -1) {

                sb.append((char)ch);}

                reader.close();

            }

            catch(Exception exception) { }

            return sb.toString();

            }

   

}

2) If I am using ESR, and I need to make all the fields at sender side as one string and map it with target side also as one string and not field by field mapping.

In ESR you will have will to create operation mapping use dummy service interface and this Java mapping. In your ID refer same dummy service interface and operation mapping.

saravanan_a
Active Participant
0 Kudos

Hi Amol,

Can you please elaborate on 1st req.

If possible steps to do.

Regards,

Saravanan

Former Member
0 Kudos

Hi Saravanan,

Refer these blogs for Java mapping. You will have to use external tools like NWDS or Eclips to create java mappings-

http://scn.sap.com/blogs/step-by-step-guide-for-jaxb-mapping/2012/03/14/step-by-step-to-learn-java-m...

http://scn.sap.com/people/community.user/blog/2008/05/05/implementing-a-java-mapping-in-sap-pi-to-re...

Import these .jar files in PI Imported Archives and which can be then directly used in Operation mapping.

-Amol

Former Member
0 Kudos


Hi Amol,

I am new to SAP PI. I want a Java code to sort the files by name. My SAP PI version is 7.1.Is there any standard module for doing this? Please help....

Former Member
0 Kudos

Hi Shruti, Do you want to sort the files before reading them in PI, Can you elaborate more on your requirement.

Former Member
0 Kudos

chk my wiki where i have used adapter module to change the file name dynamically in a file to file pass through scenario...

http://wiki.sdn.sap.com/wiki/display/Snippets/Dynamic+file+name+in+a+File+to+File+pass+through+scena...

MichalKrawczyk
Active Contributor
0 Kudos

Hi,

>>>My requirement is, I need file name at receiver side as file name+date and not file name+time stamp.

use an OS command to change the filename to the fielname+date

or adapter module (custom) to concat those two (filename + date) a few lines of code only

>>>2) If I am using ESR, and I need to make all the fields at sender side as one string and map it with target side also as one string and not field by field mapping.

use XSLT and map it to CDATA in the target field - I've written a few blogs on how to do that on SDN

Regards,

Michal Krawczyk


saravanan_a
Active Participant
0 Kudos

Hi Michal,

Can you please provide step by step process for

>>>> use an OS command to change the filename to the fielname+date

Thanks,

Saravanan

Former Member
0 Kudos

HI ,

pls refer below doc:

Run Operating System Command After Message Processing

http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=272171407

and

http://scn.sap.com/thread/2022335

http://scn.sap.com/thread/1805832

2) If I am using ESR, and I need to make all the fields at sender side as one string and map it with target side also as one string and not field by field mapping.

just concat one by one source fields to target field

or

Have to write simple Concat UDF

public String Concat(String a, String b, String c, String d, String e, Container container) throws StreamTransformationException{

String temp = ""; \\ target field

temp = a + b + c + d + e ; \\ source fields

return (temp);

Message was edited by: solasu bhavanisankar

former_member184681
Active Contributor
0 Kudos

Hi,

See this wiki for a complete comparison of all methods of dynamically assigning the file name, including OS command (config and example):

http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=272171407

Regards,
Greg

saravanan_a
Active Participant
0 Kudos

Hi Grzegorz,

I go through the wiki which you mentioned in your reply.

Can you please tell me the steps for

Run Operating System Command After Message Processing

to meet my requirement  filename+date

I find from your wiki that, for date environment variable is %DATE%

former_member184681
Active Contributor
0 Kudos

Use similar configuration as in the wiki, except for the OS command itself, which should be in your case:

cmd.exe /C "ren %F filename_%DATE%.txt"

Use your own required filename part instead of "filename" and the required file extension in the end.

Hope this helps,
Greg

RameshGulipall
Active Participant
0 Kudos

Hi Greg,

Thanks for your reply

I have added the OS command itself as you suggested above.But i got the error while calling the OS command "Error when executing OS command "cmd.exe /C ren %F Test_EXTCOMP_%DATE%.csv": Cannot run the command due to: Cannot run cmd.exe - exec failed (ENOENT - No such file or directory)."

Please provide your inputs.

Ramesh

former_member184681
Active Contributor
0 Kudos

Dear Ramesh,

I think I got the point. The operating system behind is some sort of Linux/Unix, isn't it? If yes, then the OS command should be different - my example was related to Windows environment. For instance, "ren" will have to be replaced with "rename". If you require help on building the OS command for your environment, try searching some Linux/Unix forums. Your requirement is pretty simple, so you should find a solution easily.

Hope this helps,
Greg

RameshGulipall
Active Participant
0 Kudos

Hi Greg,

Still i got the same error after changed to os command to rename.Please find the blow errorError when executing OS command "cmd.exe /C rename %F Test_EXTCOMP_%DATE%.csv": Cannot run the command due to: Cannot run cmd.exe - exec failed (ENOENT - No such file or directory).

Please suggest me correct command.

Regards,

Ramesh

former_member184681
Active Contributor
0 Kudos

I'm not a Linux specialist and do not have any Linux environment to test it, but I think the correct syntax is: rename %F 'Test_EXTCOMP_%y-%m-%d.csv', without the cmd.exe and so on at the beginning. Let me know if it works.

Greg

baskar_gopalakrishnan2
Active Contributor
0 Kudos

If your os is unix , you have to use mv command instead of rename. 

RameshGulipall
Active Participant
0 Kudos

Hi Baskar,

I am using Linux.Please provide the correct command.thnaks for  information.

Regards,

Ramesh

RameshGulipall
Active Participant
0 Kudos

Hi Baskar,

Same error i got the using mv  in command line.error is "Error when executing OS command "mv %F Test_EXTCOMP_%DATE%.csv": Cannot run the command due to: Cannot run mv %F Test_EXTCOMP_%DATE%.csv - exec failed (ENOENT - No such file or directory)."

please provide the correct command.

Regards,

Ramesh

Message was edited by: Ramesh Gulipalli

baskar_gopalakrishnan2
Active Contributor
0 Kudos

No such file or directory.

The error shows that your filename could not be found. That means first check the path of the directory upon which the file resides. After that reference the filename accordingly in the command.

Example: Use find command to check where your file resides. Or Take help from admin guys.

Refer this link

http://linux.about.com/od/commands/l/blcmdl1_find.htm

former_member184681
Active Contributor
0 Kudos

Dear Ramesh,

Firstly, I don't think %DATE% will work under Linux, as it's a Windows' environment variable. Secondly, if your system is unable to find the file, you might need to use whole path (%f) instead. Final command: rename %f 'Test_EXTCOMP_%y-%m-%d.csv'

Regards,
Greg


RameshGulipall
Active Participant
0 Kudos

HI All,

I am using linux command in OS command line after message processing.Still i have an issues with processing OS command.Please provide the any inputs and it woukld be great help.the errordetails are Error when executing OS command "mv %F Test_EXTCOMP_%DATE%.csv": Cannot run the command due to: Cannot run mv %F Test_EXTCOMP_%DATE%.csv - exec failed (ENOENT - No such file or directory).

Regards,

Ramesh

Message was edited by: Ramesh Gulipalli

former_member184681
Active Contributor
0 Kudos

mv is for moving files, not for renaming them. Did you try the syntax I mentioned before: rename %f 'Test_EXTCOMP_%y-%m-%d.csv' ?

Former Member
0 Kudos

for req 1 : use Adapter specific msg attributes, tick file name

create a UDF and generate filename dynamically : using get function to get source filename , getting its name and extension split, appending date to name and then appending the extension to it in a string variable and assign this string variable to filename using put function.

map a header node at target side with this UDF. take date as in input field to this UDF and this date should be current date function in graphical mapping.

For req 2 : take all fields from source and using concat function , send it to a single target field.

saravanan_a
Active Participant
0 Kudos

Hi Rajeev,

As you mentioned in your reply to map header node at target side. But I am not using ESR. So how can i map.. Can you please explain.

Former Member
0 Kudos

i thought both the reqs are of single scenario, so since in second u said use of ESR, so i suggested the mapping which needs ESR.

anyways if you dont have to use ESR, then the approach can use custom adapter module invoking the above UDF logic and using ASMA in receiver file comm channel.