Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Application specific solutions
    • Dataverse
    • Excel
    • Forms
    • Outlook
    • Planner
    • Power Apps
    • SharePoint
    • Teams
  • Triggers
  • Ready to use solutions
    • Approval Process Template
    • Task Delegation App
    • The Ultimate Power Automate expressions cheat sheet
    • Power Automate HTTP requests to SharePoint cheat sheet
    • Power Automate HTTP requests to Graph API cheat sheet
  • ABOUT ME
  • Get help with your flow
  • POWER PLATFORM SERVICES
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.


๐Ÿš€ Master Power Automate

Join 2,000+ professionals getting actionable Power Automate tutorials, solutions, cheat sheets & tips every week.

No spam. Unsubscribe anytime.

15 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
  6. Magno says:
    October 26, 2022 at 6:59 pm

    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.

    Reply
    1. Tom says:
      November 6, 2022 at 8:04 pm

      Hello Magno,
      I think it’s doable, but it’d need additional logic (quite complex probably) as the solution describes 1:1 copy.

      Reply
  7. Adam says:
    November 7, 2024 at 7:20 am

    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!

    Reply

Leave a Reply Cancel reply

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

๐Ÿš€ Master Power Automate

Join 2,000+ professionals getting actionable Power Automate tutorials, solutions, cheat sheets & tips every week.

No spam. Unsubscribe anytime.

Still exchanging emails to get things approved?

Use the Approval Process Template and the Task Delegation App to skip the hard part and deploy an automated, fully functional approval solution on a SharePoint list in minutes! And then the next one, and the next one...

Approval Template Preview โœจ Learn more โœจ

Turn ideas into flows with ease!

Grab the complete Power Automate Cheat Sheet Bundleโ€”everything you need to master expressions, SharePoint HTTP calls, and Graph API in Power Automate.

Cheat Sheet Bundle Preview โœจ Get the Cheat Sheets

Didn't find what you were looking for?
Need to adjust a solution to fit your needs?
Or would you just like to get an assistance from somebody with thousands of hours of experience with Power Automate?

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

©2025 Let's POWER Automate | Theme by SuperbThemes