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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / [Need Help] How to mat...
Power Automate
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!

Categories:
I have the same question (0)
  • grantjenkins Profile Picture
    11,063 Moderator on at

    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?

  • LinTse Profile Picture
    158 on at

    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.

  • Verified answer
    grantjenkins Profile Picture
    11,063 Moderator on at

    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

    Hi, @grantjenkins 

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

  • YT-16091250-0 Profile Picture
    2 on at
    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.

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 503 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 321 Moderator

#3
abm abm Profile Picture

abm abm 237 Most Valuable Professional

Last 30 days Overall leaderboard