web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Copying file to new do...
Power Automate
Answered

Copying file to new document library but maintaining folder path

(0) ShareShare
ReportReport
Posted on by

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

 

Categories:
I have the same question (0)
  • Jay-Encodian Profile Picture
    2,920 on at

    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 Profile Picture
    on at

    @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

  • Jay-Encodian Profile Picture
    2,920 on at

    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 Profile Picture
    on at

    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 Profile Picture
    on at

    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

  • Verified answer
    Jay-Encodian Profile Picture
    2,920 on at

    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

  • Rob_CTL Profile Picture
    on at

    Thanks @Jay-Encodian that works really well now.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 124 Super User 2026 Season 1

#2
Ellis Karim Profile Picture

Ellis Karim 52 Super User 2026 Season 1

#3
Haque Profile Picture

Haque 50

Last 30 days Overall leaderboard