cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Rob_CTL
Level: Powered On

Copying file to new document library but maintaining folder path

Hi,

 

I am trying to create a simple document library sync between two libraries.  Both the source and target libraries have the exact same folder structure.  What I am trying to do is when a file is created/modified in the source library a flow triggers a copy process.  Rather than having to create a flow for each folder, I am trying to manipulate the "Folder Path" of the file so that I can use it in the "Destination Folder" parameter with the "SharePoint Copy File" action.

 

flow-copy1.png

Of course doing this means that the destination folder path ends up having both the source and target document library names in the path.

 

flow-copy2.png

 

Is there a way I can strip out the source document library name from the "Folder Path" variable?

 

Thanks

Rob

 

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Copying file to new document library but maintaining folder path

Hey@Rob_CTL 

Here's an extra expression to remove the last '/' .... I have tested and this works:

Compose: replace(variables('test'),'Source','Destination')

Compose 2: substring(outputs('Compose'),0,lastIndexOf(outputs('Compose'),'/'))

You'll obviously need to change the variables... you could of course nest these expressions into a single expression but I've done this for ease of reading / understanding what's happening

1.png

 

HTH

Jay

View solution in original post

7 REPLIES 7
Super User
Super User

Re: Copying file to new document library but maintaining folder path

Hi @Rob_CTL 

Yes, you can do this with the string replace expression, example: replace('Source Library/Reports','Source Library','Target Library')

This would return a value of: Target Library/Reports

HTH

Jay

-------------------------------------------------------------------------

If I have answered your question, please mark your post as Solved.

If you like my response, please give it a Thumbs Up

 

Rob_CTL
Level: Powered On

Re: Copying file to new document library but maintaining folder path

@Jay-Encodian thanks for the information.  That works well but doesn't quite do what I'm after as it fixes the destination folder to /reports.  What I am trying to do is capture the path of the source file which might be in a sub folder then use that as the path for target (minus the source document library name) so the path is more dynamic.

 

Rob

Super User
Super User

Re: Copying file to new document library but maintaining folder path

Hi @Rob 

You just need to substitute the values with dynamic data, for example: concat(body('Get_file_properties')?['{Path}'],'Source Library','Target Library')

When you're in the expression builder, click dynamic data and select the 'Folder Path' variable.

1.png

 2.png

 

HTH

Jay

Rob_CTL
Level: Powered On

Re: Copying file to new document library but maintaining folder path

Hi @Jay-Encodian 

 

Thanks for your patience on this.  I've put the command in as you suggested and the result:

 

flow-copy3.png

 

So still not quite what I am looking to do, maybe I am over thinking this.  All I am really trying to do is remove the first part of the folder path above so instead of it reading "Source Library/Reports" it is just "/Reports".  If I can do that in a compose action and use the output to provide the copy file action it's folder path that would be brilliant.

 

Rob

 

Rob_CTL
Level: Powered On

Re: Copying file to new document library but maintaining folder path

Building on what @Jay-Encodian has provided I thought I could make this really simple and create a compose action with a simple replace function:

 

 

replace(body('Get_file_properties')?['{Path}'],'Source','Target')

 

 

That worked, well it successfully replaced the word "Source" in the folder path to "Target", but for some very odd reason when I use the compose output in the "Copy File" action "Destination Folder" it adds a / to the path

 

flow-copy4.png

This leading / causes the flow to fail with the following error:

 

 

Failed to verify the existence of destination location at 'https://xxxxx.sharepoint.com/sites/test/Target Library/Reports/' due to error 'The system cannot find the file specified. (Exception from HRESULT: 0x80070002)'.
clientRequestId: 54032ecf-1c6e-4195-a1f7-f9b3e21019e1
serviceRequestId: 54032ecf-1c6e-4195-a1f7-f9b3e21019e1

 

 

That URL is valid in that I can copy it to a browser and it opens the document library.  If I run the flow and select the folder by browsing to it the output doesn't have the / on the folder path and the flow completes.

 

Thanks in advance for any help.

Rob

Super User
Super User

Re: Copying file to new document library but maintaining folder path

Hey@Rob_CTL 

Here's an extra expression to remove the last '/' .... I have tested and this works:

Compose: replace(variables('test'),'Source','Destination')

Compose 2: substring(outputs('Compose'),0,lastIndexOf(outputs('Compose'),'/'))

You'll obviously need to change the variables... you could of course nest these expressions into a single expression but I've done this for ease of reading / understanding what's happening

1.png

 

HTH

Jay

View solution in original post

Rob_CTL
Level: Powered On

Re: Copying file to new document library but maintaining folder path

Thanks @Jay-Encodian that works really well now.

Helpful resources

Announcements
firstImage

Incoming: New and improved badges!

Look out for new contribution recognition badges coming SOON!

firstImage

Power Platform Online Conference

Join us for the first ever Power Platform Online Conference!

firstImage

New & Improved Power Automate Community Cookbook

We've updated and improved the layout and uploading format of the Power Automate Cookbook!

thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Power Automate Community Video Gallery!

Top Solution Authors
Top Kudoed Authors
Users online (5,703)