How To Grab the File Extension of an Email Attachment

I’ve been working with email triggers a lot lately. Most of my use cases involve grabbing attachments from email and saving them on a shared drive. This is simple and easy to do, but….what you might not know about Power Automate is that it interprets any image in an email as an attachment. And, to avoid processing unnecessary files, the cloud flow should include a condition that looks at the file extension. So, how do you grab the file extension of an email attachment? It’s easy. Read on to find out.

Use Case

I’ve written a whole lot about email triggers lately, so you might want to check out my post on building triggers with multiple strings and the post on how to prevent email triggers from firing when the email is a reply or a forward. For this use case, we will monitor an email inbox. If an email arrives and contains an xls, xlsx, or csv attachment, save the file on a shared drive location.

I built the cloud flow with an email trigger. Then, I use Get Attachment to grab the attachment. Now, some of you will say I don’t need Get Attachment because the attachment comes in with the trigger. Yes, the attachment does come in with the trigger. But, I’ve run into errors before when I don’t use Get Attachment. Get Attachment seems to be more reliable, so I’ve incorporated this action into my own best practices. After Get Attachment, there are two Compose actions. One of them is for demonstration purposes. If you are creating this flow on your own, add one Compose. Then, the Condition specifies which file attachments should be processed. Finally, the cloud flow saves the file.

file extension

Compose

So, what’s in the Compose actions? I use the last and split functions to extract the file extension. Split looks at the file name string and breaks it apart at the period. Last looks at the result of the split and grabs the last value that was split apart. Manuel T. Gomes has a great blog and wrote a nice post on the utility of last, especially with arrays, that you can find here.

last(split(outputs('Get_Attachment_(V2)')?['body/name'], '.'))

In order to demonstrate, I’ll run the flow and show the results after each of the Compose actions.

The output of the first compose using just the split function is an array with 2 lines.

When I add the last function into the mix, it picks up the last value in the array, which is the file extension. Now we have something to feed into the condition to process only the file extensions we care about.

get file extension

And that is how you grab the file extension from an email attachment in Power Automate. If you enjoyed this content please share it on LinkedIn or other social media.

Other Sweet Power Automate Content

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.