How to split file name for further processing in Power Automate
Posted On June 13, 2021
“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('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.
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.
And because it’s an array, you can access each value by the index: , , …
"1234", index  in the array
"Contract", index  in the array
"Tom.pdf" index  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'],'_') will give you 1234
split(outputs('Get_Attachment_(V2)')?['body/name'],'_') will give you Contract
split(outputs('Get_Attachment_(V2)')?['body/name'],'_') 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  from that split and use it for the 2nd split.
split(outputs('Get_Attachment_(V2)')?['body/name'],'.') will give you 1234_Contract_Tom
split(split(outputs('Get_Attachment_(V2)')?['body/name'],'.'),'_') will give you Tom
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.