Colin
Colin

Reputation: 3

Power Automate - Copy one SharePoint site to another but exclude a file type

So... I have an automated flow that copies files from a folder in one SharePoint site into a target folder in another site (same tenant). It is set to exclude a particular file type during copy using basically a for each loop. However, because I only have the one loop, it only does it for the root folder. How do I get it to dynamically work through nested folders? The source folder will get more folders in the future, and I would like to set the flow to run once a day.

The flow looks kinda like this:

Manually trigger a flow (for testing the flow before automating it)

List folder

Apply for each

Condition

It took me a while to get that bit to work, then I realised it wasn't iterating through the nested folders so the "pdfs" were still being copied.

Any ideas? Did I completely choose the wrong options in the flow?

Upvotes: 0

Views: 307

Answers (1)

beelow
beelow

Reputation: 111

So it sounds like you need to essentially duplicate the contents of a document library from one SharePoint site into another, excluding PDFs. The setup you have now is good all you really need to do is add another condition that checks whether the items is a folder or not, and then create the file/folder appropriately. Luckily, a recursive solution is not necessary.

The following is a general description of a flow that will copy all files (including folder structure and excluding PDFs) from one SharePoint site's document library to another SharePoint site's document library:

  1. Your flow's trigger
  2. Initialize varibale
    • Name = strTemplateLibraryName
    • Type = String
    • Value = <the name of the document library you are copying files from>
  3. Initialize variable
    • Name = strTargetLibraryName
    • Type = String
    • Value = <the name of the document library you are copying files to>
  4. Get files (properties only)
    • Site Address = <the URL of the site you are copying files from>
    • Library Name = strTemplateLibraryName
  5. Apply to each
    • Input = outputs('Get_files_(properties_only)')?['body/value']
  6. Condition (inside Apply to each)
    • items('Apply_to_each')?['{IsFolder}'] is not equal to true

The false branch of your Condition will be all iterations where the item is a folder and should have this structure:

  1. Compose (FullFolderPath)
    • Inputs = items('Apply_to_each')?['{FullPath}']
  2. Compose (FolderPath)
    • Inputs = last(split(outputs('FullFolderPath'), variables('strTemplateLibraryName')))
  3. Create new folder
    • Site Address = <the URL of the site you are copying files to>
    • List or Library = <the document library you are copying files to>
    • Folder Path = outputs('FolderPath')

The true branch of your Condition will be all iterations where the item is a file and should have this structure:

  1. Condition
    • whatever your existing condition is to check if the file is a PDF
    • if the file is not a PDF, continue steps below, else ignore
  2. Compose (FullFilePath)
    • Inputs = items('Apply_to_each')?['{FullPath}']
  3. Compose (FilePath)
    • Inputs = first(split(last(split(outputs('FullFilePath'), variables('strTemplateLibraryName'))),item()?['{FilenameWithExtension}']))
  4. Get file content
    • Site Address = <the URL of the site you are copying files to>
    • File Identifier = items('Apply_to_each')?['{Identifier}']
    • Infer Content Type (parameter) = Yes
  5. Create file
    • Site Address = <the URL of the site you are copying files to>
    • Folder Path = /variables('strTargetLibraryName')outputs('FilePath')
    • File Name = items('Apply_to_each')?['{FilenameWithExtension}']
    • File Content = body('Get_file_content')

When I made this flow a while back, I was referencing this guide that you might find helpful. It is difficult to write out flows on here so please let me know if you have any questions.

Upvotes: 0

Related Questions