Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Automate - Building Flows
Answered

[Need Help] How to matching and compare part of file name

(1) ShareShare
ReportReport
Posted on by 158

I have a document library some filename named like [filename] + [timestamp].
I'm trying to build a flow to check all filename, remain newly file, and delete old files.

LinTse_0-1702100750916.png

 

My scenario:
- Collect all files name
- Split files name to [filename] [timestamp]
- If [filename] same Compare [timestamp]
- Delete old [timestamp] file


Please teach me how to implement this scenario.
Is it possible to use [send HTTP request to SharePoint] action?


Any information will be appreciated! Thank you!

  • YT-16091250-0 Profile Picture
    2 on at
    [Need Help] How to matching and compare part of file name
    Hi , @grantjenkins 
     
    i can't progress because the pngs can't be opened after this section, is it possible to share them again? 
    Thank you.
  • LinTse Profile Picture
    158 on at
    Re: [Need Help] How to matching and compare part of file name

    Hi, @grantjenkins 

    I just test the flow and it works perfectly!!
    Thank you for teaching me and explaining it patiently.
    Thank you so much!!

  • Verified answer
    grantjenkins Profile Picture
    11,059 Super User 2025 Season 1 on at
    Re: [Need Help] How to matching and compare part of file name

    Hopefully this will get what you're after.

     

    I've got the following files in my Timestamps folder.

    grantjenkins_0-1702109074923.png

     

    See full flow below. I'll go into each of the actions.

    grantjenkins_1-1702109125349.png

     

    Get files (properties only) orders the results by Filename (FileLeafRef) in descending order so the most recent file (based on timestamp) are first. By default, Get items only returns the first 100 items. If you have more than 100 items then you can set the Top Count to 5000 (max) to retrieve up to 5000. The Order By expression is below.

     

    FileLeafRef desc

     

    grantjenkins_2-1702109289796.png

     

    Select gets a list of all the file names up to the timestamp (if no timestamp then it will return the full name).

     

    slice(item()?['{Name}'], 0, lastIndexOf(item()?['{Name}'], '_'))

     

    grantjenkins_3-1702109579594.png

     

    Apply to each iterates over the filenames using the following expression to remove duplicates.

     

    union(body('Select'), body('Select'))

     

    grantjenkins_4-1702109641613.png

     

    Filter array uses the following expression to retrieve the files that match the current file we are iterating over.

     

    slice(item()?['{Name}'], 0, lastIndexOf(item()?['{Name}'], '_'))

     

    grantjenkins_5-1702109709808.png

     

    Apply to each file to delete iterates over each of the files retrieved in the Filter array, except for the first file (the most recent file we want to keep). It uses the following expression.

     

    skip(body('Filter_array'), 1)

     

    grantjenkins_6-1702110047914.png

     

    Delete file uses the following expression so it deletes the current file we are iterating over.

     

    item()?['{Identifier}']

     

    grantjenkins_7-1702110092737.png

     

    After running the flow, we would have the following output.

    grantjenkins_0-1702110394874.png

  • LinTse Profile Picture
    158 on at
    Re: [Need Help] How to matching and compare part of file name

    Hi, @grantjenkins 
    Thank you for replied and correcting my mistake.
    I altered my sample above, thank you so much.

     

    1. I need to look at the entire timestamp including the year (12 string), and only keep the latest one.

     

    2. BI file don't have duplicate file.

     

    3. AWS.txt is a file but without timestamp, so in this case, I need to skip it.

  • grantjenkins Profile Picture
    11,059 Super User 2025 Season 1 on at
    Re: [Need Help] How to matching and compare part of file name

    From your example above, you aren't looking at the year in the filename. What if you had two files with the same name but a different year. For example "PA_Test_2023...", and "PA_Test_2024...". Would you consider these to be two different files and need to keep both, or would you rather look at the entire timestamp including the year, and only keep the latest one?

     

    Also, your BI_Test file has AWS after the timestamp. Is this something that can also happen? And if so, could you also potentially have underscores _ after the timestamp?

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

Michael Gernaey – Community Spotlight

We are honored to recognize Michael Gernaey as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 566 Super User 2025 Season 1

#2
David_MA Profile Picture

David_MA 516 Super User 2025 Season 1

#3
stampcoin Profile Picture

stampcoin 492