How to update (not replace) an existing file in Power Automate
Posted On February 17, 2021
“I don’t want Power Automate to replace the existing file, I want to update it. To upload a new version of that file, not to delete the original one.”
When creating, moving or copying file in Power Automate, you might feel limited if you want to update an existing file. ‘Create file’ doesn’t offer any solution, if file with the name already exists, it’ll just fail. ‘Move file’ or ‘Copy file’ will at least offer you some options and not fail right away, but the update is still missing. You can replace (delete) the old file, or you can upload the new file with a different name, ..or fail. But what if you want to keep the file and just update it? To upload a new version of the file?
‘Update file’ action
It’s doable, but it’s not a simple configuration in an action, you must build the functionality by yourself. It’ll require using the ‘Update item’ action to update the file content.
‘Update file’ action allows you to update the content of an existing file. You provide the ‘Identifier’ of the file and the new content, and Power Automate will create a new version of that file with the updated content. But it’ll require some preparations in the flow before you can use that action.
Check if file exists
First step is to check if the file already exists. You want to update only existing file, if the file doesn’t exist you can’t update it.
The simplest solution is to use the behaviour mentioned before: if file exists, ‘Create item’ action will fail. And you can configure the same behaviour also for ‘Copy file’ and ‘Move file’ by selecting ‘Fail this action’ if file is already there.
You can use the Create/Copy/Move file action failure as the trigger for the file update section. You’ll set the next action’s ‘Configure run after’ to run only if the previous action ‘has failed’.
Get the existing file ‘Identifier’
Since you need the ‘Identifier’ of the existing file, you must search for it in the destination site/library. ‘Get files (properties only)’ is the action running only if the creation/move/copy failed. Use a filter query to filter by the file name you just tried to use. The output will be an array with a single file.
FileLeafRef eq 'FileNameWithExtension'
Get the new file ‘content’
‘Identifier’ of the existing file is only one of the inputs for ‘Update file’ action, the next one is ‘File Content’. Content of the new file that should replace the content of the existing file, which you’ll get with the ‘Get file content’ action. Don’t forget, it must be content of the NEW file.
Update the existing file
Now it’s the time to use the ‘Identifier’ of the existing file and the ‘File Content’ of the new file in the ‘Update file’ action. Don’t worry about the ‘Apply to each’, it’s added because the ‘Get files (properties only)’ action returned an array., but since it’s an array with a single file, it’ll run only once.
Continue in the flow
This step depends on the design of your flow. If your flow ends with the ‘Update file’ action, you can skip it.
But if your flow should continue, you must use the ‘Configure run after’ once more after the file is updated. The file updating part of the process will run only if there’s an error during creation/move/copy, otherwise it’ll be skipped. In such situations, you must tell the flow where to continue even if the update is skipped. To do that, configure the next action to run also when action ‘is skipped’. It’s similar solution as when you need to disable a part of a flow.
The solution above is build on two main ideas: first, action ‘failure’ is not always a problem you must solve. In this post you can see that action ‘failure’ can be part of the flow, that you can use it to achieve the goal.
And the second idea: don’t depend only on a single action and its configuration. If some functionality is not available directly, you can probably build it by yourself combining multiple other actions.