Skip to Content
Technical Articles
Author's profile photo Mohammed Aathif

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

Assigned Tags

      17 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sugata Bagchi Majumder
      Sugata Bagchi Majumder

      Hi Aathif,
      Thanks for the blog.
      Was there a mapping involved in your scenario? or just a passthrough ?

       

      Thanks

      Sugata

      Author's profile photo Mohammed Aathif
      Mohammed Aathif
      Blog Post Author

      Hi Sugata,

       

      It is just a pass through scenario.

       

      Thanks,

      Aathif

      Author's profile photo Ricardo Viana
      Ricardo Viana

      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.

      ?

      Author's profile photo Mohammed Aathif
      Mohammed Aathif
      Blog Post Author

      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 

      Author's profile photo Ricardo Viana
      Ricardo Viana

      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,

       

      Author's profile photo Evgeniy Kolmakov
      Evgeniy Kolmakov

      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.

      Author's profile photo Ricardo Viana
      Ricardo Viana

      Hello Evgeniy,

       

      I will not be able to implement it in this way.

       

      Thanks for the explanation

       

      Regards,

       

       

      Author's profile photo Navneet Chooramani
      Navneet Chooramani

      Hi Aathif,

      I followed the steps, but getting this below error.

      Could you please let me know if there is some prerequisite that needs to be configured before?

      Author's profile photo Mohammed Aathif
      Mohammed Aathif
      Blog Post Author

      Hi Navneet,

       

      Could you please paste your script here? did you add space after sh in the communication channel

      i.e. sh <directory where script is placed>/rename.sh

      also please test your script in any linux terminal.

      Author's profile photo Navneet Chooramani
      Navneet Chooramani

      Hi Aathif,

      First, thanks for the prompt response.

      Yes I have added space in the sender communication channel.

      PFB script file, and if we are using a windows OS, is this requirement still achievable or we need to do some changes?

      Author's profile photo Mohammed Aathif
      Mohammed Aathif
      Blog Post Author

      Nope, If you are using windows OS please go for powershell script.

      you will have to use move command instead of MV.

      Author's profile photo Navneet Chooramani
      Navneet Chooramani

      Hi Aathif,

      I have written a .ps1 script for the same requirement and successfully tested on the powershell terminal;

      The file is moved to the /sender/send destination with timestamp added to the filename.

      But when running the same script from sender channel OS command line before message processing, it fails.

      Is there any specific format of writing the command for ps1 scripts.

      Thanks in advance.

      Please find below screenshots:

       

      File is moved successfully when tested in powershell.

      Author's profile photo Navneet Chooramani
      Navneet Chooramani

      But fails in PO

      Author's profile photo Mohammed Aathif
      Mohammed Aathif
      Blog Post Author

      Hello Navneet,

       

      I would suggest you to create another batch file in the windows and code like below.

      powershell "D:\FTP_FOLDER\temp\Shell\test_rename.ps1 '%1'"

      I have saved batch file as runscript.bat so in the OS command i have executed runscript.bat

      D://FTP_FOLDER//temp//Shell//runscript.bat %F

      Please check below blog on how to execute sctipt in windows 

      https://wiki.scn.sap.com/wiki/display/XI/SAP+XI+File+Adapter+OS+Command+Line+Feature

      Author's profile photo Navneet Chooramani
      Navneet Chooramani

      Hi Aathif,

      Thanks for the support, I got it running now, but when I use Move-Item command, nothing happens, but it works fine for Copy-Item.

      Any guesses on this?

      Author's profile photo Mohammed Aathif
      Mohammed Aathif
      Blog Post Author

      Hello Navneet,

       

      Good it worked for you.

      I am neither Powershell nor Unix expert so i am not sure why Move-Item command doesn't work for you.

      my guess is file may not have appropriate permissions to move from the directory (i may be wrong) so please check if the file has got all the permissions (Read, write, execute).

      Author's profile photo Navneet Chooramani
      Navneet Chooramani

      Yes I was also trying with the same command but it was failing; we also need to set SET-ExecutionPolicy RemoteSigned in Powershell(run as administrator).

      And then we were good to run powershell scripts.

      Thanks for all the help 🙂