How to check if a file exists with SAP PRO/SAP PI
Sometimes we have to check if a file has been written on a file system and perform actions based on that. With PI we usually poll a directory using the File Sender Adapter (e.g. in TEST mode if we can not delete the file). Here´s a smarter way:
- Schedule a Trigger that runs the File Check periodically (based on your requirement)
- Read the directory of the NFS/FTP/SFTP using the FileReader Adapter
- Sender: SOAP Message coming from WHINT MessageTrigger Job (of course you can use another trigger like a file sender polling in test mode with channel scheduling)
- Receiver: FileReader Query using WHINT FileReader Adapter
- Both sides use BusinessComponent “FileChecker” here
Define iFlow/IntegratedConfiguration (SOAP to FileReader):
Configure the FileReader Adapter Channel with the file name you look for:
As the FileReader Adapter reads synchronously, we have to switch from Async to Sync using the RequestResponseBean and back to Async using the RequestOnewayBean:
If the file is found, we need to have a second (Dummy) iFlow/ICO that is receiving the Response Message from the adapter but without processing it:
Set up a MessageTriggerJob in NWA / Operations / Jobs according to your needs (the period is configurable very flexible: daily / every 10 minutes / …):
If the file can not be found, the message goes into error:
VOILÁ – here is the message in error (System Error).
If you have defined alerting, an automatic E-Mail is triggered!
In case the file is found, no additional message is being generated. This behaviour can be changed if you change the routing of the Dummy iFlow.
Nice blog Adam!
However, don't forget that SAP PI has the ability to start an interface by using a trigger file. This feature has been shipped with 7.31 SP19 and 7.50 SP04, see SAP note 2300326 - New feature: Using Trigger File in File Adapter.
Thanks Nicolas, I remember it was released based on the customer influence program. However, it is "only" a way to launch a File Sender Channel based on a trigger. The solution I am highlighting shows an independent way of checking the existence of a file (e.g. to know if the month end closing report for finance has been written into). The example here is raising an error (and alert) if a certain file was NOT found, e.g. if every month on the 1st specific data is expected in a file...