How to split file name for further processing in Power Automate

“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.

Add a Comment

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