Adding custom timestamp to the file before filename in SAP PO using Shell script
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
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.
- 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
sh <directory where shell script is placed> %f
- 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
- 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
- Make sure we tick ASMA in sender because we will be using same filename in the receiver adapter.
- 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
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
- 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