Logic Apps is an Azure component that allows you to implement powerful, serverless, and scalable automated workflows. It allows data exchange with other Azure components and also on-premise data. You can develop Logic Apps using an easy to use visual designer. In a Logic App the data exchange takes place through so called connectors of which there are approximately 200+ connectors currently available and the list is still growing rapidly.
For a scheduled interface I developed in Logic Apps, I needed to check whether a file exists in Azure File Storage and then import the file content. This sounds simple, but it wasn’t because although many Actions are available for the Azure File Storage connector, checking whether a file exists isn’t one of them.
For this blog, I have created a File Share in my Azure File Storage called “dropfolder”. In this File Share the check is if a file called “input.json” is there.
One of the first thing I then tried is implementing the Actions that can be used for retrieving the file, for example “Get file content” and “Get file metadata”.
Unfortunately, none of these Actions can be used because an error will occur when the file is not available and the workflow will not continue. Although it is correct that the workflow cannot continue, it also prevents any further actions like sending notifications. Because this is not an actual error, I knew the file may not be available, it may raise some unjustified red flags.
The workaround for this problem is based on the “List files” Action. This Action returns a list of files which reside in a given File Share. When there are no files in the File Share, the output is empty and (most import) no errors are raised.
Next step, is to filter the outcome of the “List files” Action. For this, the Action called “Filter array” is used. The input of the filter is output of “List files” and the filter consists of the “equals” comparison.
If the file exists, the output of the “Filter array” is a JSON array. If the file does not exist, the output will be empty. Last step is therefore to check the “Filter array” using a “Condition”.
With the checks in place we can run the Logic App without any file present. As you can see in the image the false condition is run (see the green check) and not the true condition (a black cross).
After this I’ve placed the file in my File Share and ran the Logic App again, and now the true condition is hit.
That’s it; check whether a file exists in 3 simple steps.