Skip to Content
Technical Articles

Adding custom timestamp to the file before filename in SAP PO using Shell script

Introduction:-

 

Recently there was a need of sending file along with the custom timestamp, as we know when we tick Add timestamp in the receiver File adapter timestamp gets appended to the file with the format <FileName>-YYYYMMDD-HHMMSS but I want timestamp before filename and it must be in custom format

ie:- MMYYYY-<FileName>

Solution:-

 

We can do this within ESR mapping by using java function or we can also develop custom adapter module. I have developed a shell script which does the job without adapter module or java function in ESR, also it is pass-through interface so why to put huge efforts developing adapter module when shell script does same job by writing just 2 line of code.

  1. Create a sender File adapter enter the path where file has to be picked up and under Processing Tab enter the directory where shell script has to be executed.

The shell script will add timestamp before processing file so script has to run before processing, at the end we are passing filename dynamically to the script file using placeholder %f

Note:-%f (file name) %F (absolute file name including path)

Since we will be executing shell script. command should start with sh followed by the directory

Eg:-

sh <directory where shell script is placed> %f

 

  1. Below is the script used to add custom timestamp to the file.
#!/bin/bash
today=`date '+%m%Y'`;
mv <your_Dir>/testappl/sender/$1 <your_Dir>/testappl/sender/send/$today-$1
  • today is a variable with current time in the format MMYYYY
  • we are using MV command to move file from one directory to another along with the new timestamp
  • %1 is holding filename which we supplied in the sender adapter using %f
  • make sure you replace <your_Dir> with the actual one from where you want to move file

So basically script is just moving file from one directory to another with our custom timestamp

  1. Now in the Source tab under the source directory enter actual directory where files will be present initially, and in the advanced selection for source file add additional parameter from where file will picked and sent to target system.

Two directory above is confusing, let me explain why I used two source directory in the sender channel.

When looked at MV command in the script it is actually moving file from sender to send folder, this is because when file adapter runs it will execute OS command before file processing and OS command moves actual source directory file to additional source directory but it is not sending file to receiver adapter. I am unaware why when sender adapter poll, OS command alone executes and file is not sent to receiver adapter.

In the next poll that is after 5 sec(in my scenario) sender adapter will find a new file in send folder and process it. File from send folder is sent to target directory

  1. Make sure we tick ASMA in sender because we will be using same filename in the receiver adapter.
  2. Now let’s jump into receiver adapter configuration, in the receiver channel I have configured target directory and used FileName(accessed via ASMA) in the filename scheme

 

Testing:-

Now we place a file in the source directory

Message monitor log

OS command executed twice because sender adapter polled 2 directories.

Testing is successful, file has got timestamp before filename and in the custom format

Conclusion:-

  • It is possible to add custom timestamp using shell script in a pass-through interface.
  • Shell script is robust so performance is increased
  • Using the same method many other use case be created like if we want to archive files in multiple locations then we can simply use CP command multiple times
7 Comments
You must be Logged on to comment or reply to a post.
  • Nice trick.

     

    I would like to know if you have same code, not to add timestamp but to delete expecific content-type – application/xml or Filename via this OS commands.

    ?

    • Hi @ricardo.viana

       

      I think you are asking if we can replace file extension?  yes it is possible, using below code you can replace filename or any text.

      #!/bin/bash
      today=`date '+%b-%Y'`;
      
      filename=$1
      find=".txt"
      replace=".csv"
      renamedfile=${filename//$find/$replace} 2><your directory>/testappl/sender/shell/log/$today-$1
      mv <your directory>/testappl/sender/$1 <your directory>/testappl/sender/send/$renamedfile-$today.txt
      
      • filename is storing dynamic filename
      • renamedfile is holding renamed filename, the expression ${filename//$find/$replace} is like find and replace in java.
      • expression starting with 2> is for error logging because when something goes wrong while executing script it becomes much difficult to find the root cause in SAP PI

      for more info check below sites

      String manipulation in UNIX

      Error logging 

      • First thanks for the prompt response,

        I would like to delete one file with the name MainDocument that it’s stored in the SFTP all the time when something is written down in the SFTP server.

        Because the SFTP adapter if you select the box – StoreAttachments the adapter tries to create a MainPayload inside PI and write down the attachments with Dynamic Name plus MainDocument.

        I already aligned with a project to SFTP server don’t care much because I’m overwriting this document  (0 bytes) every new file comes, but I got curious to use these commands in the SFTP server.

        I stored in the SFTP one shell script basic:

        #!/bin/bash
        $ rm MainDocument

        Command Line:

        sh /D80CLNT30/Script.sh

        Logs: Nothing change:

        Folder:

        Also, one point I would say the Dynamic Configuration enabled in the UDF would be a better solution in case the user in the channel some restrictions in the SFTP.

        Regards,

         

        • Hi Ricardo!

          How should your script (which is to be executed on your PI system’s OS) remove that file? 🙂

          In this case you should write the script which would connect to the same SFTP site/folder and remove the file.

          Regards, Evgeniy.