“The Power Automate flow did copy all the SharePoint files and nested folders from one site and placed them all in the root folder of the destination. Is it possible to maintain the folder structure during the copy?”
When you copy files between SharePoint document libraries, it’ll completely break the folder structure. The flow will copy all the folders, including files inside these folders, which is good. But at the same time it’ll create everything from the library also in the root folder. You’ll have all the files/folders that were originally in some folder twice in the target library. Once in the right place, in a subfolder, and second time in the root folder.
Copy only items in root folder
From the behaviour it’s clear that if you copy a folder from the root folder, it’ll copy everything inside. It’ll copy all subfolders and all files in those subfolders. That means, you can copy only the folders and documents in the root folder, and Power Automate will take care of the rest.
All the files have a dynamic content ‘Folder path’ that’ll give you path to the file. This is the property you’ll use to recognise if the file or folder is in the root folder. If it’s in the root folder, its path will be only the path to the document library. If it’s deeper in the folder structure, it’ll have a longer path.
example 'Folder path' in root folder:
Shared Documents/
example 'Folder path' deep in folder structure:
Shared Documents/Folder3/Uzavrene smlouvy/
Split the ‘File path’ with the split(…) expression by the ‘/’ character. Use the output in the length(…) expression, and check how long the path is.
length(split(items('Apply_to_each')?['{Path}'],'/'))
If the length(…) is equal to 2, it’s the root folder. Otherwise it’s deep in the structure and you can ignore it. Why is the length 2? Because the split of ‘Shared Documents/’ by a ‘/’ will give you two items. ‘Shared Documents’ and ” (nothing).
Add a ‘Condition’ to your flow, use the length(…) expression is equal to 2 condition, and ‘Copy file’ only if it’s true (file/folder is in the root folder). Such flow will copy all the files only once, including their folder structure.
Note: the ‘Copy file’ action will probably end with an error The file ... does not exist.
but it’ll copy the files nevertheless. You can ignore the error or add a ‘Terminate’ action with outcome ‘Success’ if the ‘Copy files’ fails.
Summary
When you use Power Automate to copy SharePoint files in a folder structure, you can’t just copy them. You can process them folder by folder, or you can use the workaround above. Take only the files and folders in the root folder, and “abuse” the default functionality. Let the flow create the whole folder structure and copy all the files inside.
If you’d like to keep the libraries synchronised in the future, there’s a blog post with the synchronisation flow.
Could it be possible to see the full flow? I am trying to create it but an error appears and I’m stuck!
Thanks!!!
Hello Mauricio,
it’s an extension to the flow here, the only difference is in the ‘Apply to each’ where this solution has an extra ‘Condition’ to take only the files/folders in root folder.
Hello Tom, I created an entire flow including the filter query using “substring” and the condition to copy only root folders. I get the error below:
The execution of template action ‘Apply_to_each’ failed: the result of the evaluation of ‘foreach’ expression ‘value@{outputs(‘Get_files_(properties_only)’)?[‘body/value’]}’ is of type ‘String’. The result must be a valid array.
Would it be possible to know what went wrong and how to fix it? I really need this flow to work, and the other instruction on “get files” & “copy file” worked with the result of messy folders. I’ll be so grateful if you could help. Thank you!
Hello Max,
based on the error message I’d check the input of the ‘Apply to each’ action as it seems wrong. It should be only the ‘value’ output from ‘Get files (properties only)’ action.
Hello again Tom,
thank you for your tip. I recreated the flow ensuring that the “value” is placed in “apply to each” action. The flow runs successfully but it retrieves only a few files. I guess it means the folder structure might differ from your example. Would you be willing to look at the path I need to get and copy files from?
https://0.sharepoint.com/sites/address/Shared%20Documents/projectID/5.%20Deliverables
I have several projectID folders with the subfolder name containing “Deliverables”. I used the “substringof” and the length/split condition, but my flow keeps finishing with only 7 files at every run. I am so frustrated… I know that you don’t need to provide me the solution here but I’ve been wondering why it’s not working in my case.
Hello Max,
the solution is designed to take only the documents/folders on the top level, in your case that would be all the projectID folders including all subfolders and files. If you use substringof() to search for the Deliverables AND this solution at the same time, it won’t find many files as it’s designed for top level folder, but the Deliverables are a subfolder.
If you want to copy only specific subfolders, I’d be more inclined to follow the solution described here: https://tomriha.com/copy-files-between-multiple-sp-libraries-in-a-single-power-automate-flow/. Extending the configuration list by ‘SourceFolder’ and ‘TargetFolder’ columns and using them in the ‘Get files’ and ‘Copy files’ actions.
Hello Tom, thank you for this article. This is so helpful. I wanted to ask if it is possible to sync the files and folders between 2 SharePoint document libraries instead, e.g. when the files are modified or when new files are added?
Hello John,
I looked on the solution and it’s not as simple as I though, it is worth a blog post: https://tomriha.com/synchronise-sharepoint-libraries-with-folder-structure-in-power-automate/
Hi Tom, I am working in a flow that copy folder and files when a folder or/and file is created in a path. However, I have achieved only copy files by the trigger ‘When a file is created or modified’ + copy file (Using Identifier in file to copy) but the flow copies folders and file without respect the subfolders.
I really appreciated if you can suggest a flow to fix it.
Hello Luis,
take a look on this post, it should describe the solution you’re looking for: https://tomriha.com/how-to-copy-sharepoint-files-including-folder-structure-in-power-automate/
Thanks, I am going to try it
Hello,
Is there any flow that allows you to move a whole folder inside another one.
For example.
Folder: 450001(Empty)
Subfolder: 450001-AB (With content)
And the route would be 450001/450001-AB
I will be grateful to you for your support.
Hello Magno,
I think it’s doable, but it’d need additional logic (quite complex probably) as the solution describes 1:1 copy.
Hello Tom, love ya work.
I stumbled across your site and I’m hoping you can assist, I know this post is 2 years old now but I’m having issues getting my Flow to copy more than the 2nd nested folders in my flow.
I’m using an Instant cloud flow that starts from a Teams Adaptive Card.
I’m simply copying a Folder Structure for “New Projects” from one Doc lib to another but for the life of me, I can’t get around the 2 Folder deep limitation.
any suggestions would be amazing!