“Is it possible to synchronise files between 2 SharePoint document libraries with Power Automate, including the folder structure?”
There’s already a blog post how to copy files between libraries, even including the folder structure. But that was a full copy process, it takes all the files and folders and copies them. To keep two libraries synchronised you don’t want to replace all the files over and over again. You want to synchronise only the changes. If there’s a new file, maybe even in a new folder structure, it should be synchronised including the folder structure. The same should happen when a file is modified – the updated file should stay in the original path. And that’s what this post is about: how to synchronise files including their folder path?
Run the flow only for files
Start with the trigger ‘When a file is created or modified (properties only)’, and add a trigger condition to run the flow only for files.
This way you’ll simplify the flow. If you processed also the folders, you’d need two branches in the flow – one for files and one for folders. You can save one of the branches if you create the folders only when you need them, together with a file.
Copy the file
A file (not a folder) was created or modified, now it’s time to copy it. I prefer using the ‘Copy file’ action as you can easily replace the file if it already exists. This solution will replace the file, but you can also update it if you’d like.
Note: You could use also the ‘Create file’ action, but then you’d have to build the replace functionality by yourself.
The tricky part is to define the ‘Destination Folder’. While the trigger provides you the ‘Folder Path’ dynamic content, e.g.
it’s slightly different than what the ‘Copy file’ action expects. If you use the ‘Folder Path’, you’ll end up with an error message: “Failed to verify the existence of destination location at…”. That’s because the action needs the / character on the other side, the ‘Destination Folder’ must have the format below.
Add the opening / to the ‘Destination Folder’, and follow with an expression that’ll give you the path without the last /. It’ll take(…) the whole length(…) of the path without the last 1 character.
Create the folder structure (if needed)
Here comes the downside of the ‘Copy file’ action. If you used the ‘Create file’, it would automatically create the folder structure. With ‘Copy file’ this is the functionality that you must build (which I think is much easier than building the file replacement after ‘Create file’).
If the ‘Destination Folder’ doesn’t exist, the ‘Copy file’ action will fail. But that’s fine, the flow can continue even if an action fails. Add the ‘Create new folder’ action and configure it to run only if the ‘Copy file’ has failed. With this configuration it’ll run only if the folder structure doesn’t exist. If the folder structure exists, the flow will end with the ‘Copy file’ action.
Confirm the ‘run after’ configuration and go back to the action to configure it. The ‘Folder Path’ in this action is again different from the values before before. Since ‘Create new folder’ allows you to select the ‘List or Library’, you must remove the library from the folder path.
Full folder path Shared Documents/Folder/Subfolder/ Folder path needed for 'Create new folder' Folder/Subfolder/
That’s the job for the expression below: take the whole path, split(…) it by /, skip(…) the first result, and join(…) it back. It’s the same solution as when splitting string by the first space, only this time it’s /.
Once the folder is created you can copy the file again with a second ‘Copy file’ action. If the first one fails, you’ll create the folders, and copy the file again.
The Power Automate flow above will synchronise files between two SharePoint libraries including their folder structure. It’s a bit more complex than needed as there isn’t any action in Power Automate that would do everything you need. ‘Create file’ would create the necessary folder structure, but it won’t replace existing files. ‘Copy file’ on the other hand can replace a file very easily, but doesn’t create any folders. Add the various inputs for the folder paths, and a simple functionality might need quite a complex flow.
And don’t forget to handle also the deleted files.