cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic file naming!!

Former Member
0 Kudos

Hi All,

I am working on IDOC to file scenario where file is IDOC XML file i.e I need to write the IDOC in XML file and send.

Now the naming convention of target file need to be like SAPIT_I_ITHRINT001_4279_20150225122158.XML where 4279 is counter starting form 1 and increase by one when next file is written on target folder and 20150225122158 is time stamp yyyymmddHHmmss.

Now this is File with NFS adapter so even if I use variable substitution, how can I ensure that the counter_timestamp should nor reflect in result XML file.

Please suggest.

Regards,

Sachi

Accepted Solutions (0)

Answers (4)

Answers (4)

former_member183816
Active Participant
0 Kudos

Hi,

As everyone answered above, you have to store this counter value somewhere and retreive it accordingly.

One option could be Mehra's answer by using script.

Another would be, Use java mapping, store this counter in a file in PI AL11 directory. Each time you run the mapping , retreive it from that file, increase the counter and overwite the existing file. Of course you have to place the file with initial value and maintain it properly.

Sample code:

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

public class streamTest {

  public static void main(String[] args) {

   try{

  //input strean to read the file

  InputStream fis=new FileInputStream("D:/counter.txt");

  //reading file

  int i;

  String counter="";

  while((i=fis.read())!=-1)

          {

             // converts integer to character and adding to counter string

             counter=counter+(char)i;

            

            

           }

  //Form your file name here

  String fileName="myfile_"+counter+".txt";

   System.out.println(fileName);    

  OutputStream fos=new FileOutputStream ("D:/counter.txt");

  //increasing counter

  int count=Integer.parseInt(counter);

  count=count+1;

  counter=Integer.toString(count);

  //writing new counter

  fos.write(counter.getBytes());

  }

  catch(Exception e)

  {

  e.printStackTrace();

  }

  }

}

Result:  1st run:  myfile_1.txt

             2nd run: myfile_2.txt

               ......

Regards,

Ambuj

Former Member
0 Kudos

Hi Guys,

I have used below method to achieve my requirement:

The code for getFilename is:

  //Generate File Name

try

{

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

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

//Update the ASMA File Adapter attribute

conf.put(key,fileName);

}

catch (Exception e)

{

//raise an exception if the update fails

String exception = e.toString();

return exception;

}

//return the status

return dummy;

Now the issue is that for every run my counter shows the value as 1 and does not increase for next run.

The requirement is the counter should increase by one for every run, so at the time of second run when file is place the filename should have counter value as SAPIT_I_ITHRINT001_2_20150225122158.XML and

for third run when third file is placed on target folder the file name must be  SAPIT_I_ITHRINT001_3_20150225122158.XML and so on.

How can I achieve this counter requirement??

Regards,

Sachi

iaki_vila
Active Contributor
0 Kudos

Hi Dubey,


Now the issue is that for every run my counter shows the value as 1 and does not increase for next run.

For different executions you won't have your counter persisted because the execution end and the value in memory will be deleted.. If you want a counter you will need a Z table to retrieve the last counter value. In my opinion you should change your requirement with for example a timestamp to avoid duplicate names.

Regards.

former_member184948
Active Participant
0 Kudos

Hi Dubey,

What I know is that counter counts and increments the initial value as many times it is invoked in one mapping.Means If in your Message mapping that particular mapping is invoked 5 times then counter will increase 5 times. But once whole mapping is done , your file is getting created.

Now I guess your retirement is to retain the counter value for next file also, means for next time when your MM runs it should use the counter value set from last execution.

But the counter value will get reset to initial for every new mapping, since we are not storing it somewhere since its all stateless flow.In BPM we can retain values from last execution and use it in subsequent ones.

Former Member
0 Kudos

Hi Inaki and Dilip,

Your understanding is right but I cannot use BPM as our architecture does not allow.

Please help me achieve this in someway.

Also let me know that do the Add Counter option of file adapter resets daily??

Because I do not want the counter to reset anytime. The counter must go on endlessly.

Regards,

Sachi

former_member184948
Active Participant
0 Kudos

The counter will reset after execution of mapping, so be it onces a day or N number of times, depending on how many times it is invoked..

Also, in one of my projects the files names where manipulated using scripts .

In PI File channels we have options for triggering the scripts "before message processing" and "after message processing" , you may check for some unix script and add them in channels, so that as soon as the file is placed the script will change its name.

There could be other methods but I know of only this

iaki_vila
Active Contributor
0 Kudos

Hi Dubey,

You need to store the counter. You can follow Dilip approximation, or to do a RFC/File/JDBC lookup to store/retrieve the value, but you have to take into account the possibility of access concurrently to that data. I would do a RFC that works with a Z table, and in the RFC i would control the simultaneously access with enque/deque function over the table, and to develop a DO iteration while the table is blocked until that will be released.

Regards.

Bhargavakrishna
Active Contributor
0 Kudos
praveen_sutra
Active Contributor
0 Kudos

Hi Sachi,

"how can I ensure that the counter_timestamp should nor reflect in result XML file."

Just don't select the add time-stamp check box. It will not get added.

I hope you are taking care of generating the counter and time-stamp through variable substitution.

Alternatively you can very well use java/UDF to set the file name dynamically.

thanks and regards,

Praveen T