Skip to content

Let's POWER Automate

From no-code to low-code

Menu
  • Expressions
  • Filters
  • General
  • Problems
  • Triggers
  • Application specific solutions
    • Excel
    • Forms
    • Planner
    • Outlook
    • SharePoint
    • Teams
  • Resources
Menu

How to split file name for further processing in Power Automate

Posted on June 13, 2021 by Tom

“The files attached to an email have always the same file name format: ID_Type_User, how can I split it in Power Automate to get all 3 pieces of the information?”


When you process files with Power Automate, the easiest way to add more information about the file is in the file name. You can define a format that will tell you the file type, the related ID, and much more. With a predefined naming convention it’s also much easier to process the files. It doesn’t matter if it’s from an email attachment, or uploaded using drag and drop by users. You don’t have to ask users to fill out the file properties, Power Automate will do it for you. And this post will show you how to do that.

Use the split(…) expression

The Power Automate expression you’ll use is split(…). This expression will split a string by predefined character and return an array with all the pieces. The first parameter is the text/string to split, the second one is the character. For example, let’s take a file name in format ID_Type_User: 1234_Contract_Tom.pdf.

split('<string>', '<character>')

split('1234_Contract_Tom.pdf', '_') will return an array ["1234","Contract","Tom.pdf"]

Or a real example to split a file name from an email attachment.

split(outputs('Get_Attachment_(V2)')?['body/name'],'_')
Power Automate split file name

Access the specific value

As already mentioned, the split(…) expression will produce an array. Following the example from above, you’ll get the array below. Each item in the array will be one part of the file name.

[
    "1234",
    "Contract",
    "Tom.pdf"
]

And because it’s an array, you can access each value by the index: [0], [1], [2]…

[
    "1234",             index [0] in the array
    "Contract",         index [1] in the array
    "Tom.pdf"           index [2] in the array
]

You can even add the index directly into the expression to get only the desired value.

split(outputs('Get_Attachment_(V2)')?['body/name'],'_')[0]  will give you 1234
split(outputs('Get_Attachment_(V2)')?['body/name'],'_')[1]  will give you Contract
split(outputs('Get_Attachment_(V2)')?['body/name'],'_')[2]  will give you Tom.pdf

Remove the file extension

You probably noticed, that the last part of the string contains also the file extension. To remove the file extension, use another split on the last item, this time by a dot. Take only the 1st value on index [0] from that split and use it for the 2nd split.

split(outputs('Get_Attachment_(V2)')?['body/name'],'.')[0] will give you 1234_Contract_Tom

split(split(outputs('Get_Attachment_(V2)')?['body/name'],'.')[0],'_')[2] will give you Tom

Summary

This post was using split(…) expression only to split file name with Power Automate, but it’s not the only use case. You can use the same approach to split any string in a flow, just make sure that the string has always the same format.

Additionally to the split(…) expression itself, it’s also good to understand the array it produces. Because it’s an array, you can access the values directly via their index. It always starts with 0 and goes up to the length of the array minus 1.


Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Leave a Reply Cancel reply

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

Now available:

The Ultimate Power Automate expressions cheat sheet
Equip yourself with the tool to translate your thoughts into Power Automate expressions!

Do you struggle with the various expressions, conditions, filters, or HTTP requests available in Power Automate?

I send one email per week with a summary of the new solutions, designed to help even non IT people to automate some of their repetitive tasks.

All subscribers have also access to resources like a SharePoint Filter Query cheat sheet or Date expressions cheat sheet.

Zero spam, unsubscribe anytime.

Hello and welcome!

My name is Tom and I'm a business process automation consultant and Microsoft MVP living in the Czech Republic. I’ve been working with Microsoft technologies for almost 10 years, currently using mainly Power Automate, SharePoint, Teams, and the other M365 tools.

I believe that everyone can automate part of their work with the Power Automate platform. You can achieve a lot by "clicking" the flows in the designer, but you can achieve much more if you add a bit of coding knowledge. And that's what this blog is about.

To make the step from no-code Power Automate flows to low-code flows: using basic coding knowledge to build more complex yet more efficient flows to automate more of your daily tasks.

  • Format results of date calculation in SharePoint columnJune 29, 2022
  • Why is your Power Automate flow creating duplicatesJune 26, 2022
  • How to create a unique identifier in your Power Automate flowJune 22, 2022
  • How to assign custom SharePoint permission level with Power AutomateJune 19, 2022
  • Remove permissions from a specific SharePoint user with Power AutomateJune 15, 2022

Power Automate blogs worth visiting

Damien Bird
Dennis (Expiscornovus)
Paul Murana

© 2022 Let's POWER Automate | Powered by Superbs Personal Blog theme