cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
leung2019
Frequent Visitor

How to rename files to meet naming convention requirement using flow

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Pstork1
Most Valuable Professional
Most Valuable Professional

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.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

View solution in original post

15 REPLIES 15
Pstork1
Most Valuable Professional
Most Valuable Professional

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)



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
VJR
Multi Super User
Multi Super User

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. 

 

 

 

 

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!

             

 

----
If my post has answered your question, please thumbs up and mark this post as a solution.

I also offer paid consulting services at www.peakdigitran.com/appointments and you can find me on LinkedIn at https://www.linkedin.com/in/michael-annis-80903/,
leung2019
Frequent Visitor

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

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

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
Most Valuable Professional
Most Valuable Professional

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.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

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? 

Pstork1
Most Valuable Professional
Most Valuable Professional

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.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.

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.

Thank you so much for this answer! I was surprised by one of the comments that there was no action for it. 

 

Knew there would be a way to do it!

Pstork1
Most Valuable Professional
Most Valuable Professional

I agree that the REST call will work, but I stand by the comment that there is no regular action for renaming a file.  



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
DDIAS
New Member

Sadly this is true, there seems to be no easy way to rename a file.
Renaming should be a simple task and not so much a pain. Copying the file, recreating it with a new name and deleting the old one works HOWEVER:

1- It's slow

2- It's innefective

3- It may be disrupted by file size limits

4- It's 3 steps for 1 single action that already exists in the Desktop version of Power Automate.

 

Simply cannot understand why this is not yet a feature on cloud flows.

I agree on the fact that the method of copying a file just to rename it is ineffective, slow, and have disruption by file size limits. My REST call method, which does not include copying any files, is faster, more effective, and can contain more error-handling in the flow, and negates the worry for disruption by file size.

However, @Pstork1 I still agree that it is still a horrible way of having to do any of it and it should 100% be a more user-friendly built-in action, rather than a workaround.

OSKA1
New Member

Helpful resources

Announcements

Community will be READ ONLY July 16th, 5p PDT -July 22nd

Dear Community Members,   We'd like to let you know of an upcoming change to the community platform: starting July 16th, the platform will transition to a READ ONLY mode until July 22nd.   During this period, members will not be able to Kudo, Comment, or Reply to any posts.   On July 22nd, please be on the lookout for a message sent to the email address registered on your community profile. This email is crucial as it will contain your unique code and link to register for the new platform encompassing all of the communities.   What to Expect in the New Community: A more unified experience where all products, including Power Apps, Power Automate, Copilot Studio, and Power Pages, will be accessible from one community.Community Blogs that you can syndicate and link to for automatic updates. We appreciate your understanding and cooperation during this transition. Stay tuned for the exciting new features and a seamless community experience ahead!

Summer of Solutions | Week 4 Results | Winners will be posted on July 24th

We are excited to announce the Summer of Solutions Challenge!    This challenge is kicking off on Monday, June 17th and will run for (4) weeks.  The challenge is open to all Power Platform (Power Apps, Power Automate, Copilot Studio & Power Pages) community members. We invite you to participate in a quest to provide solutions to as many questions as you can. Answers can be provided in all the communities.    Entry Period: This Challenge will consist of four weekly Entry Periods as follows (each an “Entry Period”)   - 12:00 a.m. PT on June 17, 2024 – 11:59 p.m. PT on June 23, 2024 - 12:00 a.m. PT on June 24, 2024 – 11:59 p.m. PT on June 30, 2024 - 12:00 a.m. PT on July 1, 2024 – 11:59 p.m. PT on July 7, 2024 - 12:00 a.m. PT on July 8, 2024 – 11:59 p.m. PT on July 14, 2024   Entries will be eligible for the Entry Period in which they are received and will not carryover to subsequent weekly entry periods.  You must enter into each weekly Entry Period separately.   How to Enter: We invite you to participate in a quest to provide "Accepted Solutions" to as many questions as you can. Answers can be provided in all the communities. Users must provide a solution which can be an “Accepted Solution” in the Forums in all of the communities and there are no limits to the number of “Accepted Solutions” that a member can provide for entries in this challenge, but each entry must be substantially unique and different.    Winner Selection and Prizes: At the end of each week, we will list the top ten (10) Community users which will consist of: 5 Community Members & 5 Super Users and they will advance to the final drawing. We will post each week in the News & Announcements the top 10 Solution providers.  At the end of the challenge, we will add all of the top 10 weekly names and enter them into a random drawing.  Then we will randomly select ten (10) winners (5 Community Members & 5 Super Users) from among all eligible entrants received across all weekly Entry Periods to receive the prize listed below. If a winner declines, we will draw again at random for the next winner.  A user will only be able to win once overall. If they are drawn multiple times, another user will be drawn at random.  Individuals will be contacted before the announcement with the opportunity to claim or deny the prize.  Once all of the winners have been notified, we will post in the News & Announcements of each community with the list of winners.   Each winner will receive one (1) Pass to the Power Platform Conference in Las Vegas, Sep. 18-20, 2024 ($1800 value). NOTE: Prize is for conference attendance only and any other costs such as airfare, lodging, transportation, and food are the sole responsibility of the winner. Tickets are not transferable to any other party or to next year’s event.   ** PLEASE SEE THE ATTACHED RULES for this CHALLENGE**   Week 1 Results: Congratulations to the Week 1 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Community MembersNumber SolutionsSuper UsersNumber Solutions Deenuji 9 @NathanAlvares24  17 @Anil_g  7 @ManishSolanki  13 @eetuRobo  5 @David_MA  10 @VishnuReddy1997  5 @SpongYe  9JhonatanOB19932 (tie) @Nived_Nambiar  8 @maltie  2 (tie)   @PA-Noob  2 (tie)   @LukeMcG  2 (tie)   @tgut03  2 (tie)       Week 2 Results: Congratulations to the Week 2 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 2: Community MembersSolutionsSuper UsersSolutionsPower Automate  @Deenuji  12@ManishSolanki 19 @Anil_g  10 @NathanAlvares24  17 @VishnuReddy1997  6 @Expiscornovus  10 @Tjan  5 @Nived_Nambiar  10 @eetuRobo  3 @SudeepGhatakNZ 8     Week 3 Results: Congratulations to the Week 3 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 3:Community MembersSolutionsSuper UsersSolutionsPower Automate Deenuji32ManishSolanki55VishnuReddy199724NathanAlvares2444Anil_g22SudeepGhatakNZ40eetuRobo18Nived_Nambiar28Tjan8David_MA22   Week 4 Results: Congratulations to the Week 4 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 4:Community MembersSolutionsSuper UsersSolutionsPower Automate Deenuji11FLMike31Sayan11ManishSolanki16VishnuReddy199710creativeopinion14Akshansh-Sharma3SudeepGhatakNZ7claudiovc2CFernandes5 misc2Nived_Nambiar5 Usernametwice232rzaneti5 eetuRobo2   Anil_g2   SharonS2  

Check Out | 2024 Release Wave 2 Plans for Microsoft Dynamics 365 and Microsoft Power Platform

On July 16, 2024, we published the 2024 release wave 2 plans for Microsoft Dynamics 365 and Microsoft Power Platform. These plans are a compilation of the new capabilities planned to be released between October 2024 to March 2025. This release introduces a wealth of new features designed to enhance customer understanding and improve overall user experience, showcasing our dedication to driving digital transformation for our customers and partners.    The upcoming wave is centered around utilizing advanced AI and Microsoft Copilot technologies to enhance user productivity and streamline operations across diverse business applications. These enhancements include intelligent automation, AI-powered insights, and immersive user experiences that are designed to break down barriers between data, insights, and individuals. Watch a summary of the release highlights.    Discover the latest features that empower organizations to operate more efficiently and adaptively. From AI-driven sales insights and customer service enhancements to predictive analytics in supply chain management and autonomous financial processes, the new capabilities enable businesses to proactively address challenges and capitalize on opportunities.    

Updates to Transitions in the Power Platform Communities

We're embarking on a journey to enhance your experience by transitioning to a new community platform. Our team has been diligently working to create a fresh community site, leveraging the very Dynamics 365 and Power Platform tools our community advocates for.  We started this journey with transitioning Copilot Studio forums and blogs in June. The move marks the beginning of a new chapter, and we're eager for you to be a part of it. The rest of the Power Platform product sites will be moving over this summer.   Stay tuned for more updates as we get closer to the launch. We can't wait to welcome you to our new community space, designed with you in mind. Let's connect, learn, and grow together.   Here's to new beginnings and endless possibilities!   If you have any questions, observations or concerns throughout this process please go to https://aka.ms/PPCommSupport.   To stay up to date on the latest details of this migration and other important Community updates subscribe to our News and Announcements forums: Copilot Studio, Power Apps, Power Automate, Power Pages

Users online (1,236)