cancel
Showing results for 
Search instead for 
Did you mean: 
Reply

Condition to see if a File exists within a folder

Hi community!

 

I have been making a flow that Gets the last 25 emails in a folder, uses substring to get a certain part of the body of the email, then checks a file location using the substring as the end of the file path to see if the attachment on the email already exists or not, and if not creates the file using the attachment name and attachment content. I don't know why is isn't working!!! I have done everything in my power to try and figure it out but cannot. I have even added a step to create the file if the list files in folder does not work, but it seems to do that step every-time. I have attached photos of the flow, let me know if you need anymore info 🙂 The create file actions work as they do create them, the sub string works as it gets the correct string. It seems to just be the condition.

Screenshot 2023-12-08 160556.png

Screenshot 2023-12-08 160657.png

  

1 ACCEPTED SOLUTION

Accepted Solutions

@SamMartin The logic in your flow needs some adjusting. The nested Apply to Each actions aren't necessary. 

 

Instead, it would be more efficient to collect the substrings from your all emails first than

loop through those to check if a file exists. Remember to rename your actions to keep things organized.

 

Get Emails

In the advanced option of the Get Emails (V3) action you can specify the top count. This will limit the number of emails returned to the number you input here. I would recommend putting a low number here while you are building and testing your flow. The max number of emails this action can return at a time is 25. 

 

Select No for the Fetch Only Unread Messages—if you want to return messages that have already be read in your inbox. Otherwise, this action will only look for unread emails. You can include attachments as well.

 

If you only want to return emails with attachments—you'll need to select Yes for the Only with Attachments dropdown. 

creativeopinion_51-1702085614786.png

Loop through Each Email

Add an Apply to Each action and insert the value output from the Get Emails (V3) action. This action will loop through each email. 

creativeopinion_4-1702081126582.png

Get Substring

You can use the substring action or an expression in a Compose action—I'll use a Compose action and an expression.

Add a Compose action to your flow.

creativeopinion_7-1702081251591.png

 

In the Expression tab, enter the string() function. 

creativeopinion_11-1702081719336.png

 

Click on the Dynamic content tab and insert the body dynamic content from the Get Emails (V3) action into the string() function.

creativeopinion_12-1702081743718.png

Place your cursor at the start of the expression by pressing the up arrow key and type in substring with an opening bracket.

substring(

creativeopinion_13-1702081776541.png

 

Go to the end of the expression by pressing the down arrow key and add a comma and the start index integer—922 in your case. A tooltip will appear above indicating which parameter the expression is expecting.

creativeopinion_14-1702081853096.png

 

 

Add another comma and the length integer—6 in your case.

creativeopinion_15-1702081875300.png

Add a closing bracket and run a test. Verify the outputs are what you expect.

creativeopinion_16-1702081956360.png

 

Check if File Exists

I'm using a Get files (properties only) action in my case—here you can insert your List Files In Folder action. I'm using a Filter Query to limit the items returned to files only as this particular action does return folders as well. 

creativeopinion_23-1702082495874.png

 

Count Files Returned

For troubleshooting purposes, add a Compose action with an expression to return the number of files returned from your action. 

 

Use the length() function and insert the values dynamic content from the action above.

creativeopinion_18-1702082280847.png

Run a test.

creativeopinion_21-1702082436627.png

 

Get File Names of Files in Folder

Add a Select action to your flow. Insert the Value dynamic content from the Get files (properties only) action. 

 

Click on the icon to change from map to text mode.

creativeopinion_27-1702082933406.png

Insert the File name with extension dynamic content from the Get files (properties only) action. This action will return an array of all the file names of the files in the specified folder.

 

creativeopinion_28-1702082996663.png

Next, add a Join action. Insert the output from the Select action above. In the Join with field insert a comma with a space after it.

creativeopinion_30-1702083123619.png

 

This action will convert the array of file names into a string of file names. Run a test. 

creativeopinion_31-1702083820125.png

 

Filter Array on Attachments

The Attachments dynamic content returns an array of attachments. Use the Filter Array action to check if any of those attachments exist in your folder already. This is more efficient than looping through each attachment and running a condition check to see if the file exists. 

 

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

 

In the From field, insert the Attachments dynamic content.

creativeopinion_24-1702082654644.png

In the first value field, insert the output from the Join action (the one that has the attachment file names from your folder in a comma separated string).

 

Change the operator to does not contain.

 

In the second value field, insert the Attachments Name dynamic content from the Get Emails (V3) action. Important: Power Automate will automatically nest the Filter Array action inside another Apply to Each action. This is NOT required. 

creativeopinion_32-1702083868023.png

 

Drag your Filter Array action outside of the Apply to Each action that was automatically added—delete it.

creativeopinion_34-1702083960592.png

 

Add a Compose action to store the number of items in your Filter Array action. Run a test.

creativeopinion_35-1702084049975.png

 

In my example, I have an email with 3 attachments. One of those attachments already exist in my folder.

creativeopinion_36-1702084322574.png

creativeopinion_37-1702084338258.png

The Filter Array action essentially cross-checks the file names of my attachments in the current email (being looped through) to see if those files exist already. If they do, the Filter Array action will filter that item/those items out.

 

The Filter Array action filtered out that item, returning only two files. 

creativeopinion_38-1702084377655.png

 

Group Actions (optional)

To keep things organized, you can use a Scope action to group actions together. This will allow you to quickly collapse actions with a single click. This is totally optional—however I prefer to use them to keep my flows organized.

creativeopinion_39-1702084513979.png

 

Condition Check

Add a Condition action. In the first value field, insert the output from the Compose action with the Filtered Files count.

creativeopinion_40-1702084638857.png

Change the operator to is not equal to and enter 0 into the second value field. This condition will check if there are any filtered files before continuing on with your flow. Add the rest of your actions to the YES branch.

creativeopinion_52-1702085762325.png

 

Loop Through Filtered Array

Add an Apply to Each action to loop through your Filtered Array of attachments.

creativeopinion_41-1702084952965.png

 

The next step is optional. I'll be inserting a Compose action to store the Attachment ID, Name and Content from the Filter array action. Alternatively, you can insert the expressions right into the Create File action—I prefer to use Compose actions.

 

Compose Dynamic Content from Filter Array

Add a Compose action to store the Attachment ID. Alternatively you can insert the expression directly into the Get Attachment (V2) action. I prefer to use Compose actions.

 

If you look at the output of the Filter Array action you can access the dynamic content from it by using an expression and referencing a key. The key is the red text between the quote marks.

 

Please refer to this section of my YT Tutorial that covers how to return the dynamic content from the Filter Array action. 

 

creativeopinion_53-1702085859245.png

 

To return the Attachment Id, you'll need to use the item() function. Add a question mark and square brackets with single quotes.

 

item()?['']

In between the single quotes insert the key (red text between double quotes).

 

creativeopinion_46-1702085151765.png

Run a test. The

creativeopinion_54-1702086047005.png

 

Repeat this step for the name and attachment content (contentBytes).

creativeopinion_59-1702086627264.png

creativeopinion_60-1702086652465.png

 

 

Create File(s)

Add a Create File action. I'm using the Create File SharePoint action—you'll need to select the proper action for your flow requirements. 

 

In the folder path—I've inserted the output from the Compose action with the substring. 

creativeopinion_56-1702086246858.png

In the File Name field, I've inserted the output from the Compose action that has the Attachment Name. For the File Content field, I've inserted the output from the Compose action that has the Attachment Content.

 

creativeopinion_62-1702086766078.png

 

Run a test. The two attachments returned from the Filter Array action have been created.

 

 

creativeopinion_58-1702086473441.png

 

 

I have a whole tutorial on how to save attachments from email onto SharePoint—you can reference that tutorial as well. Most of the concepts covered above are also covered in that tutorial.

 

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

 

View solution in original post

5 REPLIES 5

@SamMartin The logic in your flow needs some adjusting. The nested Apply to Each actions aren't necessary. 

 

Instead, it would be more efficient to collect the substrings from your all emails first than

loop through those to check if a file exists. Remember to rename your actions to keep things organized.

 

Get Emails

In the advanced option of the Get Emails (V3) action you can specify the top count. This will limit the number of emails returned to the number you input here. I would recommend putting a low number here while you are building and testing your flow. The max number of emails this action can return at a time is 25. 

 

Select No for the Fetch Only Unread Messages—if you want to return messages that have already be read in your inbox. Otherwise, this action will only look for unread emails. You can include attachments as well.

 

If you only want to return emails with attachments—you'll need to select Yes for the Only with Attachments dropdown. 

creativeopinion_51-1702085614786.png

Loop through Each Email

Add an Apply to Each action and insert the value output from the Get Emails (V3) action. This action will loop through each email. 

creativeopinion_4-1702081126582.png

Get Substring

You can use the substring action or an expression in a Compose action—I'll use a Compose action and an expression.

Add a Compose action to your flow.

creativeopinion_7-1702081251591.png

 

In the Expression tab, enter the string() function. 

creativeopinion_11-1702081719336.png

 

Click on the Dynamic content tab and insert the body dynamic content from the Get Emails (V3) action into the string() function.

creativeopinion_12-1702081743718.png

Place your cursor at the start of the expression by pressing the up arrow key and type in substring with an opening bracket.

substring(

creativeopinion_13-1702081776541.png

 

Go to the end of the expression by pressing the down arrow key and add a comma and the start index integer—922 in your case. A tooltip will appear above indicating which parameter the expression is expecting.

creativeopinion_14-1702081853096.png

 

 

Add another comma and the length integer—6 in your case.

creativeopinion_15-1702081875300.png

Add a closing bracket and run a test. Verify the outputs are what you expect.

creativeopinion_16-1702081956360.png

 

Check if File Exists

I'm using a Get files (properties only) action in my case—here you can insert your List Files In Folder action. I'm using a Filter Query to limit the items returned to files only as this particular action does return folders as well. 

creativeopinion_23-1702082495874.png

 

Count Files Returned

For troubleshooting purposes, add a Compose action with an expression to return the number of files returned from your action. 

 

Use the length() function and insert the values dynamic content from the action above.

creativeopinion_18-1702082280847.png

Run a test.

creativeopinion_21-1702082436627.png

 

Get File Names of Files in Folder

Add a Select action to your flow. Insert the Value dynamic content from the Get files (properties only) action. 

 

Click on the icon to change from map to text mode.

creativeopinion_27-1702082933406.png

Insert the File name with extension dynamic content from the Get files (properties only) action. This action will return an array of all the file names of the files in the specified folder.

 

creativeopinion_28-1702082996663.png

Next, add a Join action. Insert the output from the Select action above. In the Join with field insert a comma with a space after it.

creativeopinion_30-1702083123619.png

 

This action will convert the array of file names into a string of file names. Run a test. 

creativeopinion_31-1702083820125.png

 

Filter Array on Attachments

The Attachments dynamic content returns an array of attachments. Use the Filter Array action to check if any of those attachments exist in your folder already. This is more efficient than looping through each attachment and running a condition check to see if the file exists. 

 

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

 

In the From field, insert the Attachments dynamic content.

creativeopinion_24-1702082654644.png

In the first value field, insert the output from the Join action (the one that has the attachment file names from your folder in a comma separated string).

 

Change the operator to does not contain.

 

In the second value field, insert the Attachments Name dynamic content from the Get Emails (V3) action. Important: Power Automate will automatically nest the Filter Array action inside another Apply to Each action. This is NOT required. 

creativeopinion_32-1702083868023.png

 

Drag your Filter Array action outside of the Apply to Each action that was automatically added—delete it.

creativeopinion_34-1702083960592.png

 

Add a Compose action to store the number of items in your Filter Array action. Run a test.

creativeopinion_35-1702084049975.png

 

In my example, I have an email with 3 attachments. One of those attachments already exist in my folder.

creativeopinion_36-1702084322574.png

creativeopinion_37-1702084338258.png

The Filter Array action essentially cross-checks the file names of my attachments in the current email (being looped through) to see if those files exist already. If they do, the Filter Array action will filter that item/those items out.

 

The Filter Array action filtered out that item, returning only two files. 

creativeopinion_38-1702084377655.png

 

Group Actions (optional)

To keep things organized, you can use a Scope action to group actions together. This will allow you to quickly collapse actions with a single click. This is totally optional—however I prefer to use them to keep my flows organized.

creativeopinion_39-1702084513979.png

 

Condition Check

Add a Condition action. In the first value field, insert the output from the Compose action with the Filtered Files count.

creativeopinion_40-1702084638857.png

Change the operator to is not equal to and enter 0 into the second value field. This condition will check if there are any filtered files before continuing on with your flow. Add the rest of your actions to the YES branch.

creativeopinion_52-1702085762325.png

 

Loop Through Filtered Array

Add an Apply to Each action to loop through your Filtered Array of attachments.

creativeopinion_41-1702084952965.png

 

The next step is optional. I'll be inserting a Compose action to store the Attachment ID, Name and Content from the Filter array action. Alternatively, you can insert the expressions right into the Create File action—I prefer to use Compose actions.

 

Compose Dynamic Content from Filter Array

Add a Compose action to store the Attachment ID. Alternatively you can insert the expression directly into the Get Attachment (V2) action. I prefer to use Compose actions.

 

If you look at the output of the Filter Array action you can access the dynamic content from it by using an expression and referencing a key. The key is the red text between the quote marks.

 

Please refer to this section of my YT Tutorial that covers how to return the dynamic content from the Filter Array action. 

 

creativeopinion_53-1702085859245.png

 

To return the Attachment Id, you'll need to use the item() function. Add a question mark and square brackets with single quotes.

 

item()?['']

In between the single quotes insert the key (red text between double quotes).

 

creativeopinion_46-1702085151765.png

Run a test. The

creativeopinion_54-1702086047005.png

 

Repeat this step for the name and attachment content (contentBytes).

creativeopinion_59-1702086627264.png

creativeopinion_60-1702086652465.png

 

 

Create File(s)

Add a Create File action. I'm using the Create File SharePoint action—you'll need to select the proper action for your flow requirements. 

 

In the folder path—I've inserted the output from the Compose action with the substring. 

creativeopinion_56-1702086246858.png

In the File Name field, I've inserted the output from the Compose action that has the Attachment Name. For the File Content field, I've inserted the output from the Compose action that has the Attachment Content.

 

creativeopinion_62-1702086766078.png

 

Run a test. The two attachments returned from the Filter Array action have been created.

 

 

creativeopinion_58-1702086473441.png

 

 

I have a whole tutorial on how to save attachments from email onto SharePoint—you can reference that tutorial as well. Most of the concepts covered above are also covered in that tutorial.

 

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

 

Hi @creativeopinion  thanks for looking into this with me 🙂

I notice using this it will not loop through each of the emails it retrieves in the Get Emails V3, therefore would it need to be nested in a loop so that it moves onto the next email and gets the next substring? 

SamMartin_0-1702058125766.png

There should be 10 different values here but this value is the one on the first email, it isn't getting the rest from the other 10 emails?

@SamMartin This select action doesn't require to be nested inside an Apply to Each loop. The From value is the value dynamic content from the Get emails (V3) action—essentially the items returned from that action. 

 

You should check your Get Emails (V3) action to ensure the top count of emails being returned as well as only including email with attachments.

 

I've limited my top count to 3 for testing purposes. I would recommend you do the same to speed up your flow runs while you are building and testing your flow. 

creativeopinion_0-1702061182019.png

 

Will be adding additional instructions shortly... 

@SamMartin I've updated the instructions. I realized after reviewing your requirements again that I made an error in my original recommendation. I've updated the original post. Please take a look. Hope it helps!

@creativeopinion Thanks for this massively! This worked, had to do a bit of tweaking to adjust it for my file system but it was very inline with your method. The only thing I noticed was when I tried using a compose the get the substring in the array it would still only result with the first emails substring 3 times rather than the 3 emails substrings, I changed it to the substring action and it worked perfectly 🙂 I have marked as solution.

Helpful resources

Announcements

Community will be READ ONLY July 16th, 5p PDT -July 22nd

Dear Community Members,   We'd like to let you know of an upcoming change to the community platform: starting July 16th, the platform will transition to a READ ONLY mode until July 22nd.   During this period, members will not be able to Kudo, Comment, or Reply to any posts.   On July 22nd, please be on the lookout for a message sent to the email address registered on your community profile. This email is crucial as it will contain your unique code and link to register for the new platform encompassing all of the communities.   What to Expect in the New Community: A more unified experience where all products, including Power Apps, Power Automate, Copilot Studio, and Power Pages, will be accessible from one community.Community Blogs that you can syndicate and link to for automatic updates. We appreciate your understanding and cooperation during this transition. Stay tuned for the exciting new features and a seamless community experience ahead!

Summer of Solutions | Week 4 Results | Winners will be posted on July 24th

We are excited to announce the Summer of Solutions Challenge!    This challenge is kicking off on Monday, June 17th and will run for (4) weeks.  The challenge is open to all Power Platform (Power Apps, Power Automate, Copilot Studio & Power Pages) community members. We invite you to participate in a quest to provide solutions to as many questions as you can. Answers can be provided in all the communities.    Entry Period: This Challenge will consist of four weekly Entry Periods as follows (each an “Entry Period”)   - 12:00 a.m. PT on June 17, 2024 – 11:59 p.m. PT on June 23, 2024 - 12:00 a.m. PT on June 24, 2024 – 11:59 p.m. PT on June 30, 2024 - 12:00 a.m. PT on July 1, 2024 – 11:59 p.m. PT on July 7, 2024 - 12:00 a.m. PT on July 8, 2024 – 11:59 p.m. PT on July 14, 2024   Entries will be eligible for the Entry Period in which they are received and will not carryover to subsequent weekly entry periods.  You must enter into each weekly Entry Period separately.   How to Enter: We invite you to participate in a quest to provide "Accepted Solutions" to as many questions as you can. Answers can be provided in all the communities. Users must provide a solution which can be an “Accepted Solution” in the Forums in all of the communities and there are no limits to the number of “Accepted Solutions” that a member can provide for entries in this challenge, but each entry must be substantially unique and different.    Winner Selection and Prizes: At the end of each week, we will list the top ten (10) Community users which will consist of: 5 Community Members & 5 Super Users and they will advance to the final drawing. We will post each week in the News & Announcements the top 10 Solution providers.  At the end of the challenge, we will add all of the top 10 weekly names and enter them into a random drawing.  Then we will randomly select ten (10) winners (5 Community Members & 5 Super Users) from among all eligible entrants received across all weekly Entry Periods to receive the prize listed below. If a winner declines, we will draw again at random for the next winner.  A user will only be able to win once overall. If they are drawn multiple times, another user will be drawn at random.  Individuals will be contacted before the announcement with the opportunity to claim or deny the prize.  Once all of the winners have been notified, we will post in the News & Announcements of each community with the list of winners.   Each winner will receive one (1) Pass to the Power Platform Conference in Las Vegas, Sep. 18-20, 2024 ($1800 value). NOTE: Prize is for conference attendance only and any other costs such as airfare, lodging, transportation, and food are the sole responsibility of the winner. Tickets are not transferable to any other party or to next year’s event.   ** PLEASE SEE THE ATTACHED RULES for this CHALLENGE**   Week 1 Results: Congratulations to the Week 1 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge.   Community MembersNumber SolutionsSuper UsersNumber Solutions Deenuji 9 @NathanAlvares24  17 @Anil_g  7 @ManishSolanki  13 @eetuRobo  5 @David_MA  10 @VishnuReddy1997  5 @SpongYe  9JhonatanOB19932 (tie) @Nived_Nambiar  8 @maltie  2 (tie)   @PA-Noob  2 (tie)   @LukeMcG  2 (tie)   @tgut03  2 (tie)       Week 2 Results: Congratulations to the Week 2 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 2: Community MembersSolutionsSuper UsersSolutionsPower Automate  @Deenuji  12@ManishSolanki 19 @Anil_g  10 @NathanAlvares24  17 @VishnuReddy1997  6 @Expiscornovus  10 @Tjan  5 @Nived_Nambiar  10 @eetuRobo  3 @SudeepGhatakNZ 8     Week 3 Results: Congratulations to the Week 3 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 3:Community MembersSolutionsSuper UsersSolutionsPower Automate Deenuji32ManishSolanki55VishnuReddy199724NathanAlvares2444Anil_g22SudeepGhatakNZ40eetuRobo18Nived_Nambiar28Tjan8David_MA22   Week 4 Results: Congratulations to the Week 4 qualifiers, you are being entered in the random drawing that will take place at the end of the challenge. Week 4:Community MembersSolutionsSuper UsersSolutionsPower Automate Deenuji11FLMike31Sayan11ManishSolanki16VishnuReddy199710creativeopinion14Akshansh-Sharma3SudeepGhatakNZ7claudiovc2CFernandes5 misc2Nived_Nambiar5 Usernametwice232rzaneti5 eetuRobo2   Anil_g2   SharonS2  

Check Out | 2024 Release Wave 2 Plans for Microsoft Dynamics 365 and Microsoft Power Platform

On July 16, 2024, we published the 2024 release wave 2 plans for Microsoft Dynamics 365 and Microsoft Power Platform. These plans are a compilation of the new capabilities planned to be released between October 2024 to March 2025. This release introduces a wealth of new features designed to enhance customer understanding and improve overall user experience, showcasing our dedication to driving digital transformation for our customers and partners.    The upcoming wave is centered around utilizing advanced AI and Microsoft Copilot technologies to enhance user productivity and streamline operations across diverse business applications. These enhancements include intelligent automation, AI-powered insights, and immersive user experiences that are designed to break down barriers between data, insights, and individuals. Watch a summary of the release highlights.    Discover the latest features that empower organizations to operate more efficiently and adaptively. From AI-driven sales insights and customer service enhancements to predictive analytics in supply chain management and autonomous financial processes, the new capabilities enable businesses to proactively address challenges and capitalize on opportunities.    

Updates to Transitions in the Power Platform Communities

We're embarking on a journey to enhance your experience by transitioning to a new community platform. Our team has been diligently working to create a fresh community site, leveraging the very Dynamics 365 and Power Platform tools our community advocates for.  We started this journey with transitioning Copilot Studio forums and blogs in June. The move marks the beginning of a new chapter, and we're eager for you to be a part of it. The rest of the Power Platform product sites will be moving over this summer.   Stay tuned for more updates as we get closer to the launch. We can't wait to welcome you to our new community space, designed with you in mind. Let's connect, learn, and grow together.   Here's to new beginnings and endless possibilities!   If you have any questions, observations or concerns throughout this process please go to https://aka.ms/PPCommSupport.   To stay up to date on the latest details of this migration and other important Community updates subscribe to our News and Announcements forums: Copilot Studio, Power Apps, Power Automate, Power Pages

Users online (388)