Site icon @WonderLaura

Flow Matching File Name in Library

This is a quick post about using Power Automate to copy specific files from library to library.  In my use case, I’ve got a bunch of training files that I work on, and then share with my students.  I have a library where I work on the files, and then a different library (in a different SharePoint site) where the files are shared with the students.  When I make changes to the files, I copy them over to the library for the students.  I decided to automate this, and set it to run as a weekly flow.  But I didn’t want it to copy just all of the files in the library, just the ones that have been modified since the last time they were copied over.  First of all, that might be inefficient and take too long, and secondly, I want the students to be able to easily see the “modified” date column or even set up alerts on the library to be notified when files have been updated.

Here’s how I did it.  Copy files in Library A to Library B, looking at each file to see if it has been modified in the last week.

1.  My trigger is recurrence.

2.  Initialize a variable that will be used to hold the last file modified date of the file in library B.

3.  Add the SharePoint action Get files (properties only)

Bonus: For this action, you could narrow it down so that it only gets the files that have been modified in the last 7 days, with this syntax.  Modified is greater than addDays(utcNow(), –7)

4.  Add an Apply to each loop, to loop through all of these files.  The whole rest of the flow will be inside of this loop.

5.  HERE’S the TRICK.  This system name for the filename is fairly obscure, but this how you can compare the filename of the library you’re copying to. Library B.  Query to get just that one specific file in the other library.  Set the top count to 1, since there will be just one file with that name anyway.
FileLeafRef equals File name with extension, and don’t forget the single quotes, as seen here.  This action is also Get files (properties only)

6.  Next, add an action to set a variable.  Pick the varOldFileModified.  For the value, pick the Modified date of the file from “get matching old file” above.  As soon as you do this, it will apply a loop.  Even though you are really only retrieving a single file, it will add a loop, which is fine.

7.  Add a condition.  Pick the modified date from the “Get new files” (Library A).  That modified date is greater than the varOldFileModified variable.  This means that the file in Library A has been modified more recently than the file with the same filename in Library B.

8.  In the condition’s If Yes path, add the action Copy file. Pick the identifier from the For each Library A (Library A).  Replace it if it is already there.

What about new files?  If you’d like the flow to automatically copy over files that exist in Library A, but don’t exist in Library B, you can make a change to that last condition at step 7. 

Add an OR condition, basically if there’s nothing in the “just one matching file” array. Length is an expression that lets you know how many rows are in an array / collection / list of stuff.  So, if it is looking for a matching file in library B at step 5 and doesn’t find one, that means it’s new in library A.

Exit mobile version