Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Application specific solutions
    • Dataverse
    • Excel
    • Forms
    • Outlook
    • Planner
    • Power Apps
    • SharePoint
    • Teams
  • Triggers
  • Ready to use solutions
    • Approval Process Template
    • Task Delegation App
    • The Ultimate Power Automate expressions cheat sheet
    • Power Automate HTTP requests to SharePoint cheat sheet
    • Power Automate HTTP requests to Graph API cheat sheet
  • ABOUT ME
  • Get help with your flow
  • POWER PLATFORM SERVICES
Menu

Add a date to file name when storing it in SharePoint with Power Automate

Posted on March 13, 2022March 13, 2022 by Tom

“I’d like to add a date to the name of the file received by an email, how can I inject it in there with Power Automate?”


When you store email attachments in a SharePoint document library, you might encounter files with the same file name. If the names are not unique, and you don’t handle this situation, the flow will fail.

You’ve got three ways how to solve this. The first one is to update the stored file. Keep the file including its version history and replace only its content. The second option is to replace the file. Remove the existing one, and upload the new one. And the third one is renaming the file to make the name unique. You can add the date at the beginning of the file name, or you can add it at the end. And that’s the topic of this post – adding date at the end of a file name.

Get the file name and the extension

To add a date in the file name, you must split it into two parts. The first part is the file name without the extension, and the second part is the extension. The date will then go between these two parts.

Get the file name

Let’s get the file name first. The approach will be similar as when splitting any string, starting with the split(…) expression to split the whole file name by a dot.

split(<attachment name>,'.')

Since the last item in the array will be always the file extension, take(…) all the pieces excluding the last one. That means the length(…) -1.

take(split(<attachment name>,'.'),add(length(split(<attachment name>,'.')),-1))

Then just join(…) the file name again into a string in case there were any other dots in the file name.

join(take(split(<attachment name>,'.'),add(length(split(<attachment name>,'.')),-1)),'.')

Note: < … > is a placeholder, replace it with the file name including the < and >.

That’s the file name without the extension.

Get the file extension

The same approach can be used also for the file extension. Start by splitting it again by the dot. But this time you don’t need to calculate the length, you can simply take only the last(…) piece of the array = the extension.

last(split(<attachment name>,'.'))

Build the new file name

Once you have the file name without extension, and the extension, you can build the final file name. Add the current date in between with the concat(…) and utcNow(…) expressions. Don’t forget to add the dot before the extension!

concat(<name without extension>, '_', utcNow(), '.', <extension>)

With the two ‘Compose’ actions it might look as below. You can also add some formatting to the date if you don’t want the full ISO string.

concat(outputs('Compose'), '_', utcNow(), '.', outputs('Compose_2'))
Power Automate add date file name

Summary

If you use Power Automate to create files in the same SharePoint library, and you expect they might have the same name, it’s a good idea to add a date to the file name. Split the file name into two pieces, the file name without extension and the extension, and inject the date/time in between. Such file name will be always unique and you don’t have to deal with updating/replacing the existing files.

And if you encounter corrupted attachments, take a look on the previous post.


πŸš€ Master Power Automate

Join 2,000+ professionals getting actionable Power Automate tutorials, solutions, cheat sheets & tips every week.

No spam. Unsubscribe anytime.

2 thoughts on “Add a date to file name when storing it in SharePoint with Power Automate”

  1. Emilee Nowack says:
    September 25, 2024 at 3:49 pm

    This works great! One issue I have found is that even though it’s creating a new file, the other columns are taking on the same metadata as the last entry. How do I make sure that the new added file will default to the values that I have set in SharePoint? Example would be Status should be “Not Started” but it’s coming in as “Completed” and the People field is populated as the last value entered instead of being blank as my defaults are set in SPO. I cant figure out where this is happening, do you have an idea?

    Reply
  2. guy says:
    November 1, 2024 at 5:21 pm

    Do you guys really think this is a realistic way to do this in 2024. This should be as easy as [datetime]+[filename]. How dumb to have to break apart the file name and data/time and then create 5 variables to hold this information and then piece it back together. Microsoft is so lame.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

πŸš€ Master Power Automate

Join 2,000+ professionals getting actionable Power Automate tutorials, solutions, cheat sheets & tips every week.

No spam. Unsubscribe anytime.

Working on an Approval process?

Use the Approval Process Template and the Task Delegation App to skip the hard part and deploy a fully functional approval solution on a SharePoint list in minutes! And then the next one, and the next one...

Approval Template Preview ✨ Learn more ✨

Turn ideas into flows with ease!

Grab the complete Power Automate Cheat Sheet Bundleβ€”everything you need to master expressions, SharePoint HTTP calls, and Graph API in Power Automate.

Cheat Sheet Bundle Preview ✨ Get the Cheat Sheets

Didn't find what you were looking for?
Need to adjust a solution to fit your needs?
Or would you just like to get an assistance from somebody with thousands of hours of experience with Power Automate?

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

©2025 Let's POWER Automate | Theme by SuperbThemes