How to write a condition ‘if file was found’ in Power Automate
Posted On November 29, 2020
“I am using Power Automate action ‘Get Files (properties only)’ with a Filter Query to find the file, but how can I write a condition to test if a file actually was found?”
The ‘Get files (properties only)’ action returns an array. If a file (or more files) was found, the array will contain the file, otherwise it’ll be empty. With that, you’ve got two options, depending on what you want to achieve. Do you need to do some action when there’s a file, or when there’s no file?
Action only when ‘file was found’
To process file(s) found by the ‘Get items’ action you don’t necessarily need a ‘Condition’. You can use the existing action ‘Apply to each’ that kind of includes the evaluation ‘if array is empty’. If you input an empty array into ‘Apply to each’, it has nothing to process. Each = nothing, the whole loop, including all actions inside, will be skipped.
Action when ‘file was not found’
The other option is to use ‘Condition’ action. As already mentioned, the file(s) returned by ‘Get files’ action are in an array. If there’s isn’t any file, the array is empty. I already wrote a post on using empty values in conditions, so in short, empty array = . But you can’t use that directly in the ‘Condition’.
You must bypass the validation by using one of the options below.
Empty array variable
Empty array variable, created just for this condition. Compare the ‘value’ output of ‘Get files’ with the empty variable and that’s it. Decide what should happen if there’s no file ([value] is equal to [empty array]) and when there is a file ([value] is not equal to [empty array]).
Another option is to use the length([value]) expression in the condition. When using length() on an array with files, it’ll return how many files the array contains. If it’s greater than 0, there’s at least 1 file. If it’s not greater than 0, the array is empty = there’s no file.
Both of the options above achieve similar goal, with a small difference though. The first option, using ‘Apply to each’ is better for situations when you need to process the file(s) (if they exist). It can be preparation to attach them to an email notification, or to a task.
The second option, using a direct ‘Condition’ is a simple check if there’s a file or not. It doesn’t matter if it’s one or more, you just need to confirm that some file exists. No further processing necessary if it does.