web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Save email attachments...
Power Automate
Answered

Save email attachments based on attachment size

(0) ShareShare
ReportReport
Posted on by 6

Hello -

I receive auto-generated email reports with the only difference between them is the attachment size. Each email has one of three different attachment sizes. My goal is to have Power Automate look at the size of the attachment and move it to a specific OneDrive folder based on that. Each email has either a 12kb, 25kb or 40kb attachment. The example below is what I tried for an email with a 40kb attachment.

Mike_S_0-1702074006528.png

Mike_S_1-1702074076824.png

It manages to save a file in the correct folder on OneDrive, however the attachment is corrupt and unable to be opened. The file size is also smaller than what's in the original attachment. Any help will be appreciated. Thanks!

 

Categories:
I have the same question (0)
  • creativeopinion Profile Picture
    10,502 Super User 2025 Season 2 on at

    @Mike_S I have a different approach to your scenario. To follow along you'll need to switch to the Classic Designer.

    creativeopinion_0-1702087142803.png

     

    Tip: Rename your actions to keep your flow organized!

    creativeopinion_10-1702089421778.png

     

     

    When a New Email Arrives

    Click on Show advanced options. Ensure that you've selected Yes for Include attachments and Only with attachments. This way, your flow will continue to run only if the email has attachments.

    creativeopinion_16-1702091665646.png

     

    If you'd like to speed up your flow building process—I'd recommend replacing the automated trigger with a manual trigger. You can watch this section of a YT tutorial to guide you on how to do this. This way you don't need to send yourself an email each time while you build the flow, rather you can manually trigger the flow on an email already in your inbox.

    Filter Attachments Under 40kb

    First, you'll need to use a Filter Array action to filter out any attachments under 40kb. Since your requirement is to save attachments that are 12kb, 25kb or 40kb — you should filter out the any attachments over 40kb first.

     

    To learn more about how to use the Filter Array action, please refer to this recent YT Tutorial I uploaded.

     

    Add a Filter Array action after your flow trigger. In the From field, insert the Attachments dynamic content from the flow trigger. Note: I'm using a Get email (V2) action in my flow for demo purposes.

     

    In the first value field, insert the Attachment Size dynamic content. Change the operator to is less than or equal to. In the second value field you can insert the number of bytes you'd like the attachment to be less than or equal to. Attachment sizes are returned in bytes so you will need to convert 40kb in to bytes. 

     

    creativeopinion_6-1702089139596.png

    I'm going to use an expression to calculate this. Enter the mul() function into the expression field. 

    creativeopinion_7-1702089249393.png

    The mul() function takes two numbers and multiplies them. Enter two numbers separated by a comma. In this case 40, 1024. 

     

    creativeopinion_8-1702089302454.png

    creativeopinion_9-1702089380282.png

    Return Count of Attachments

    Add a Compose action to return the count of filtered attachments. In the expressions tab, insert an expression and use the length() function. 

     

    creativeopinion_2-1702088893403.png

    Click on the dynamic content tab and insert the body dynamic content from the Filter Array action above.

    creativeopinion_3-1702088915746.png

    creativeopinion_5-1702088930458.png

    Run a test. Verify the number of attachments that have been filtered. 

    creativeopinion_11-1702089528518.png

    Initialize a String Variable

    Add an Initialize Variable action to your flow. give it a name and select String as the type. A variable is like a container and for your scenario—it'll hold the folder name. We'll set the name of the folder based on your conditions (file size).

    creativeopinion_13-1702091340325.png

     

    Condition Check

    Add a Condition action to your flow. 

     

    In the first value field, insert the output from the compose action above (the one that has the number of items returned from the Filter Array action).

     

    Change the operator to is not equal to and in the second value field insert a zero.

    creativeopinion_14-1702091471238.png

    This condition action will check to see whether or not any attachments are equal to or under 40kb. If so, the flow will continue on. All your remaining actions can go into the YES branch. If you want any actions to take place if there are attachments but none of them are 40kb or under in size—you can add those actions to the NO branch.

     

    Apply to Each Attachment

    Add an Apply to Each action to your flow and insert the body dynamic content from the Filter Array action. 

    creativeopinion_17-1702091793702.png

     

    Get Attachment Dynamic Content

    Add a Compose action. I like to use Compose actions to return the dynamic content from a Filter Array action. This can help with troubleshooting. You'll need to get the key for the dynamic content you wan to return.

     

    Refer to this section of a YT Tutorial I uploaded on how to get dynamic content from a Filter Array action.

     

    You can get the dynamic content key from the Filter Array action outputs. The key is the red text between the double quote marks. In this case, you want to return the size of the attachment. The key is 'size'

    creativeopinion_18-1702091888045.png

     

    Insert an expression below. 

    item()?['']

    creativeopinion_19-1702091984499.png

    Between the single quotes, you'll need to insert a key. In this case, it's 'size'

    item()?['size']

    Add another Compose action to store the Name and Content Byes. Follow the steps above. Important: The keys are case sensitive. 

    creativeopinion_26-1702092890923.png

     

    Run a test. Confirm the Compose actions are returning all the dynamic content.

    creativeopinion_27-1702092989231.png

     

    creativeopinion_28-1702092999551.png

    Switch + Set Variable

    Add a Switch action to your flow.

     

    A Switch action is a control that allows you to add branching paths based the value entered in the switch. Each branching path you add is referred to as a case. The value entered in a case must match the value in the switch.

     

    In the On field, insert the dynamic content from the Compose action above.

    creativeopinion_21-1702092294521.png

     

    You'll need to create a case for each file size you are looking to match. Remember to rename your cases to keep things organized. You'll need to multiply your kb values by 1024 to get the total bytes.

     

    Add a Set Variable action. Set the variable with the appropriate folder name. In my case I'm calling it 12kb.

    creativeopinion_22-1702092438222.png

     

    Add a new case by clicking the + button. Create a case for each of your requirements. Set the variable in each case.

    creativeopinion_23-1702092490853.png

     

    If there is no match to your cases, you will need to set the variable in the default case. This way your attachments will go somewhere (to another folder if they don't match. 

    creativeopinion_24-1702092601777.png

    Create File

    In the Folder Path, select a folder (if applicable) and add a forward slash right before you insert your variable. Remember that the variable (aka folder name) will be set in the switch action based on the file size of the attachment.

    creativeopinion_25-1702092734934.png

    In the File Name field, insert the outputs from the Compose action that has the attachment name.

     

    creativeopinion_29-1702093068332.png

    creativeopinion_30-1702093083115.png

     

    12kb, 25kb and 40kb are very specific file sizes. My file sizes do not match those requirements which is why my files will be saved in the Temp folder (which is what the variable was set to in the default case).

    creativeopinion_31-1702093259329.png

    The Temp folder didn't exist in my OneDrive (which is why it was automatically created).

    creativeopinion_32-1702093290901.png

     

    All my attachments ended up there.

    creativeopinion_33-1702093300674.png

     

    Hope this helps!


    If I helped you solve your problem—please mark my post as a solution ✅.
    Consider giving me a 👍 if you liked my response!

    👉Watch my tutorials on YouTube
    👉Tips and Tricks on TikTok

     

     

     

  • Mike_S Profile Picture
    6 on at

    Thank you so much for the detailed response. I'm close, but not quite there. I got all the way to the end and realized that each of the attachments that appeared to be the same size are actually bytes different. So even though it says the size is 25KB on an email, the actual byte size varies slightly. In the case below, it's 2 difference. How can I do an "in between" instead of "equals" so I can cover for any minor size discrepancies?

    Mike_S_0-1702102075916.png

     

  • Verified answer
    creativeopinion Profile Picture
    10,502 Super User 2025 Season 2 on at

    @Mike_S So in this case you don't need the Switch + Variable. You can use an expression in that case. You can delete these actions.

    creativeopinion_2-1702141246026.png

     

    Remove Create File Action (Temp)

    I would recommend temporarily removing the Create File action in your flow. Click on the three dot and select Copy to my clipboard and then delete it. This way you can test the flow to ensure the output of the expression above is correct before you even bother creating the file.

    creativeopinion_8-1702142779565.png

     

    Convert Attachment Size from Bytes to KB

    First, we'll need to convert the attachment size from bytes to kb. We'll need to divide the attachment size by 1024 to return the kb value.

     

    Unfortunately, Power Automate turns the expression labels into non-editable labels. Not sure why this is (this wasn't the case before they implemented the new designer). 

    creativeopinion_1-1702142245981.png

    However, you can access the expression with this trick. Click on the three dots of the Compose action and select Peek Code.

    creativeopinion_3-1702142319094.png

    Select the expression which is the text after the @ symbol and before the closing double quote marks. Copy it to your clipboard.

    creativeopinion_4-1702142369356.png

    Remove the dynamic content label and click on the expression tab and paste your expression in. 

    creativeopinion_5-1702142443564.png

    Wrap the expression in a div() function. Place your cursor at the start of your expression by pressing the up arrow key and insert div and an opening bracket.

    creativeopinion_6-1702142518609.png

     

    Go to the end of the expression by pressing the down arrow key and insert a comma and the number 1024 and a closing bracket.

    creativeopinion_7-1702142705810.png

     

    This expression will take the attachment size and convert it to kb. Run a test. Review the outputs.

    creativeopinion_9-1702142883849.png

     

    Compose Folders

    Add Compose actions to your flow to hold the names of each of the folders you'd like to direct your attachments to based on their size. Using Compose actions for the folder names is optional—however, I recommend this in your case to make it easy to adjust the folder name (should you need to in the future) rather than having to edit the expression.

    creativeopinion_1-1702141172353.png

     

     

    I've put my actions into a Scope action to make it easy to collapse. This is optional (not required for the flow itself).

    creativeopinion_0-1702140914996.png

     

    Set Folder Name

    Since the Switch action requires an exact match—for this particular scenario I think it would be better to use an expression to set the folder name. We'll be composing an expression with the if() + lessOrEquals() function.

     

    The if() function takes 3 parameters:

    • an expression that returns a boolean value (aka true/false)
    • true value
    • false value (optional)

    creativeopinion_3-1702141665449.png

     

    The lessOrEquals() function takes two parameters. The value your are checking and 

    creativeopinion_0-1702142120026.png

     

     

    We'll be nesting 3 if() functions + lessOrEquals() function within each other to create the expression. The logic for the expression is as follows:

     

    • If the attachment size is 12kb and under
      • true: output the 12kb and under folder name
      • false: If the attachment size is 25kb and under
        • true: output the 25kb and under folder name
        • false: If the attachment size is 40kb and under
          • true: output the 40kb and under folder name
          • false: output the default folder name.

     

    Tip: Each function needs an opening and closing bracket. Go slow an ensure you are always entering a closing bracket to ensure you aren't missing anything. If you ever have issues and get an alert saying your expression is invalid—throw it into ChatGPT! Sometimes I find it easier to build expressions from the inside out (so I don't forget a bracket). Do whatever works for you.

     

    Additionally to quickly get to the start of the expression: Press the UP arrow key. To get to the end of the expression: Press the DOWN arrow key. Pay attention to the tooltip as it'll bold the text of which parameter of the function you are setting.

     

    Add a Compose action. Click on the Expression tab and insert this expression:

    if(lessOrEquals(outputs('Compose_-_Attachment_Size'), 12), outputs('Compose_-_12kb'), 

    if(lessOrEquals(outputs('Compose_-_Attachment_Size'), 25), outputs('Compose_-_25kb'),

    if(lessOrEquals(outputs('Compose_-_Attachment_Size'), 40), outputs('Compose_-_40kb'),

    outputs('Compose_-_Default'))))

    Depending on how you've named your Compose actions in your flow, you may need to replace the dynamic content references in my expression with the appropriate dynamic content references in yours.

     

    • You will need to replace the blue output in my expression with the output from the Compose action that is storing the attachment size kb output from your flow 
    • You will need to replace the red output in my expression with the output from the appropriate Compose action in your flow. 

    Once the text is highlighted, in the Dynamic content tab and select the appropriate Compose action output.

     

    Run a test. Review the outputs. You can see in my screenshots below the first loop of the Apply to Each action has an attachment that is 8kb, so the output of the folder name is 12kb Folder. The second loop has an attachment that is 15kb and the output of the folder name is 25kb.

     

    creativeopinion_30-1702147299073.png

    creativeopinion_31-1702147312672.png

     

    Create File

    Add the Create File action back to your flow. Click on Add an action then click on My clipboard. Select the Create file action. 

    creativeopinion_32-1702147400165.png

    Replace the variable reference with the output of the compose action above. 

    creativeopinion_33-1702147478108.png

    creativeopinion_34-1702147494803.png

    Run a test!

     

    creativeopinion_35-1702147563005.png

     

    Now if you choose to rename your folders, you can edit the Compose actions instead of having to edit the expression.

    creativeopinion_36-1702147773876.png

     

    Hope this helps!


    If I helped you solve your problem—please mark my post as a solution ✅.
    Consider giving me a 👍 if you liked my response!

    👉 Watch my tutorials on YouTube
    👉 Tips and Tricks on TikTok

     

  • Mike_S Profile Picture
    6 on at

    That seemed to work perfectly as intended! Thank you so much for taking the time to respond. I especially appreciated the screenshots, as I'm more visual. Thanks again. 🙂

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Automate

#1
Michael E. Gernaey Profile Picture

Michael E. Gernaey 525 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 324 Moderator

#3
abm abm Profile Picture

abm abm 232 Most Valuable Professional

Last 30 days Overall leaderboard