“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.
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.
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.