Skip to content

Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Problems
  • Triggers
  • Application specific solutions
    • Excel
    • Forms
    • Planner
    • Outlook
    • SharePoint
    • Teams
  • Resources
Menu

How to copy SharePoint files including folder structure in Power Automate

Posted on July 7, 2021January 12, 2022 by Tom

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

Power Automate copy SharePoint files folder

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.


Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

12 thoughts on “How to copy SharePoint files including folder structure in Power Automate”

  1. Mauricio Florez says:
    August 13, 2021 at 1:52 am

    Could it be possible to see the full flow? I am trying to create it but an error appears and I’m stuck!

    Thanks!!!

    Reply
    1. Tom says:
      August 14, 2021 at 8:46 am

      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.

      Reply
  2. Max says:
    October 18, 2021 at 5:47 am

    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!

    Reply
    1. Tom says:
      October 18, 2021 at 10:43 am

      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.

      Reply
  3. Max says:
    October 19, 2021 at 1:09 pm

    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.

    Reply
    1. Tom says:
      October 22, 2021 at 10:30 am

      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.

      Reply
  4. John says:
    January 6, 2022 at 3:39 am

    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?

    Reply
    1. Tom says:
      January 12, 2022 at 5:22 pm

      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/

      Reply
  5. Luis says:
    February 8, 2022 at 11:08 pm

    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.

    Reply
    1. Luis says:
      February 8, 2022 at 11:09 pm

      I really appreciated if you can suggest a flow to fix it.

      Reply
      1. Tom says:
        February 9, 2022 at 5:38 pm

        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/

        Reply
        1. Luis says:
          February 10, 2022 at 2:18 pm

          Thanks, I am going to try it

          Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Now available:

The Ultimate Power Automate expressions cheat sheet
Equip yourself with the tool to translate your thoughts into Power Automate expressions!

Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Hello and welcome!

My name is Tom and I'm a business process automation consultant and Microsoft MVP living in the Czech Republic. I’ve been working with Microsoft technologies for almost 10 years, currently using mainly Power Automate, SharePoint, Teams, and the other M365 tools.

I believe that everyone can automate part of their work with the Power Automate platform. You can achieve a lot by "clicking" the flows in the designer, but you can achieve much more if you add a bit of coding knowledge. And that's what this blog is about.

To make the step from no-code Power Automate flows to low-code flows: using basic coding knowledge to build more complex yet more efficient flows to automate more of your daily tasks.

  • Format results of date calculation in SharePoint columnJune 29, 2022
  • Why is your Power Automate flow creating duplicatesJune 26, 2022
  • How to create a unique identifier in your Power Automate flowJune 22, 2022
  • How to assign custom SharePoint permission level with Power AutomateJune 19, 2022
  • Remove permissions from a specific SharePoint user with Power AutomateJune 15, 2022

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

© 2022 Let's POWER Automate | Powered by Superbs Personal Blog theme