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 / How to rename files to...
Power Automate
Answered

How to rename files to meet naming convention requirement using flow

(1) ShareShare
ReportReport
Posted on by 34

In our SharePoint Online libraries, there are thousands of files without consistent naming convention, some files with the date in at the end, such as QualityControl_2018-08-20, the others with the date in at the front, such as 2018-08-20_QualityRecord, and some without date. Instead of manual rename them one by one, Is there a way to rename these files with consistent naming convention using flow, i.e. places the date end of the file name in all documents?

I have the same question (0)
  • Pstork1 Profile Picture
    69,129 Most Valuable Professional on at

    Here's a walkthrough on how to use Power Automate Desktop to rename files.  IN your case the hard part will be figuring out what the file name should be, especially when there is a filename that doesn't include a date.  How will the flow know what date to include in the filename?

    How to use Power Automate to systematically rename files (techrepublic.com)

  • VJR Profile Picture
    7,635 on at

    There are 3 types of files as per your inputs.

     

    • Files with date at the end: QualityControl_2018-08-20
    • Files with date at beginning: 2018-08-20_QualityRecord
    • Files without any date: QualityControl  

     

    - Let us call the Input string as FileName 

    - Take the FileName and use the Split Text action.

       Custom character to split on will be the underscore character _

    • Split Text returns a List type of variable called TextList....something like the below

    VJR_0-1671159827940.png

     

    • Use the ParseText action on the 0th element. Use regex to check if the date is at the beginning or at the end or no date at all. 

    ([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))

    The regex worked fine under basic tests. Please test this regex using any free online regex tester (https://regex101.com/)

     

       VJR_2-1671160119816.png

     

    •  If the Position variable returns 0 or greater, then the date was found. The resulting date will be saved in the Match variable shown above. This will be used to rename the file to the way you need it.

       In this case the portion of text other than the date is in the 1st element ie; %TextList[1]%.

       Now you can rename the file as desired using the %Match% and the %TextList[1]%. Instead of Match you can also use %TextList[0]%

                       Now you can move on to the next file since you have already done renaming this file.

     

    • But if the Position returned is -1 then no date was found in the 0th element %TextList[0]%.

                    Now in the ParseText pass the 1st element %TextList[1]% to check if it is a case         

                     like QualityControl_2018-08-20.

                     This is a valid case and you want it just like this.

                     So no changes needed and you can move on to the next file.

     

    • If Match is blank on the original string called FileName then it means it is the 3rd case like no date - QualityControl

             Take this above string and append the date you want (probably the file created or modified date) and 

              rename the file as per your requirements.

     

     

               Looking at the explanation seems difficult, but it is not that much.

     

    The Idea is to:

    - Split based on underscore _   

    - Check which part has the date using Regex

    • If 1st part is a date- Save both the parts and rename the file accordingly
    • When the 2nd part ie; the 1st element already has a date then no action needed
    • If no date at all in the original string then take the original string and append the desired date while renaming the file.

     

    Please write back in case you are facing any issues. Lets get you till the final completion. 

     

     

     

     

  • MichaelAnnis Profile Picture
    5,727 Moderator on at

    get files

    copy %Files% to clipboard

    For each %File% in %Files%

        If %File.Name% contains _ ‘(an underscore)

            If %File.Name% starts with 2
                Split text custom character (underscore)

                Set %NewName% to %File[1]%_%File[0]%

                Rename %File% to %NewName%

            End (IF)

            Convert Date to Text %File.LastModified% to yyyy-MM-dd and save as %NewDate%

            Set %NewName% to %File.Name%_%NewDate%

            Rename %File% to %NewName%

        End (IF)

     

    In plain English, this says: if filename has an underscore, we know a date exists. If it starts with 2, then flip the date and the rest decided by the underscore. if it doesn’t start with a 2, do nothing, because it should be correct. 

    if there was no underscore, we assume there is no date and use the date last modified to add on to the end of the filename. 

    good luck!

                 

     

  • leung2019 Profile Picture
    34 on at

    Thanks all for the reply. I am still working on this after the break of holidays

  • leung2019 Profile Picture
    34 on at

    I am using Microsoft 365 ( Microsoft Flow), do I have to install the desktop version? Can I just using online version ?  

  • leung2019 Profile Picture
    34 on at

    I am using Microsoft 365 ( Microsoft Flow), do I have to install the desktop version? Can I just using online version ?  

    as below 

    Flow.PNG

     

  • Pstork1 Profile Picture
    69,129 Most Valuable Professional on at

    There is no action in Power Automate that will let you rename existing files.  You would need to copy the file and rename it as part of the copy.  Then delete the original file.

  • leung2019 Profile Picture
    34 on at

    Thanks. We need to copy thousands of files, and paste them in a new document library in SharePoint Online. The name of these files are not consistent with naming convention, So we need to rename files, can we use power automate to rename them during copy process? 

  • Verified answer
    Pstork1 Profile Picture
    69,129 Most Valuable Professional on at

    To do that you'll need to retrieve the content of the existing file and create a new one in the new location.  When you create the new file you will be able to specify any name you want.  But you can only do it using the Create file action.  There is no action to rename files.

  • RT-25060746-0 Profile Picture
    on at

    This is actually incorrect. You can rename files through an HTTP request to SharePoint, and it works for Lists and Libraries.

     

    For demonstration, here is my Power Automate flow:

    In my case, I am changing the internal SharePoint library {FilenameWithExtension} property when someone edits a file. (There is a trigger condition so it doesn't run infinitely, but that's irrelevant.) 

    In this instance I have a custom column field that is a placeholder for the name of the item that is safely editable by users in the library. Theoretically editing my '{fake file name}' field is when the flow would trigger.

    PAFlowExample1.png

    Here are my pseudo-steps:

    1. File is modified (Trigger condition: {Title} is not equal to {fake file name})
    2. Change {Title} to {fake file name}, passing the triggerOutputs()?['body/ID'] from the previous step to the Id field.

     

    PAFlowExample2.png

     3. Then I check if my {fake file name} field is not equal to the internal {FilenameWithExtension} property, because if it isn't then that's when I want to change the internal file name to my new file name that the user obviously changed it to in the {fake file name} field.

    If they are equal then I don't care about changing the internal file name in this flow because it wasn't the name they changed.

    PAFlowExample3.png

     

    4. From here, I follow the steps on this user blog. I Send an HTTP request to SharePoint (a Power Automate built in action). As per the blog, "This [step] will return the ListItemEntityTypeFullName property, which we will need for the next step."

    You'll notice that here I use the {GetByID} call and plug in my library's ID. The reason for this is because when I use the blog's suggested {GetByTitle} call I got this error:
    "List '<Your list/library name>' does not exist at site with URL: ''<my domain> dot sharepoint dot com/sites/CEOPortalTest" "

    This fix is here (second last answer). You basically go to your Library Settings and check out your actual URL in the browser. Between %7B and %7D will be the ID of your Library/List. For some reason ID works and GetByTitle doesn't.



    5. In the final step, you're basically passing in the file ID from an earlier step, using the generic ['body/ID'] will not work. This specifies the exact file ID the HTTP request is modifying.

    Then you're entering the new properties of the file in that specific format, as per the blog. Honestly this part goes over my head a little but hey, it works and it's clear to those who understand it.

    PAFlowExample4.png

     

    And that's it! It works beautifully. Here's proof (Watch the name of the left field, which is the internal library {FilenameWithExtension} property.

     

    PAFlowExample5.png

     

    I'm changing the {fake file name} field on this document to X_HR_EmployeeChanged_050221.docx.

     

    PAFlowExample6.png

     

    Once the change has been made, I exit out of edit mode (grid view in SharePoint Online). You can see the {fake file name} field has changed and the internal file name has not, at least not yet. Now we wait a couple of seconds for the flow to run.

     

    PAFlowExample7.png

     

    And there you can see the flow kicked off and passed successfully:

    PAFlowExample8.png

     

    And there's your proof! Clear as day, a renamed internal file from a SharePoint Online List/Library:

     

    PAFlowExample9.png

     

    I hope this helps someone looking for the same solution I was.

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!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Haque Profile Picture

Haque 605

#2
Valantis Profile Picture

Valantis 340

#3
11manish Profile Picture

11manish 284

Last 30 days Overall leaderboard