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 / Create a sub-folder in...
Power Automate
Unanswered

Create a sub-folder in document library if folder does not exist

(0) ShareShare
ReportReport
Posted on by 76

Hi all,

 

I have a flow that saves email attachments to a specific document library folder. The folder structure is contained in the email body and 90% of the time it is the same folder but there are occasions when the sub-folder does not exist. I'd like to know if it is possible, using power automate to check if the folder exists and create one if it does not. 

 

Is this something i should be able to achieve with Power Automate? If anyone has done this already, your help would be greatly appreciated.

 

Thanks advance,

Syed

Categories:
I have the same question (0)
  • Expiscornovus Profile Picture
    33,189 Most Valuable Professional on at

    Hi @syed_sme,

     

    Yes, that would be possible by using an Send an HTTP request to SharePoint action and using the Folders method with a filter. If the outputs of that action is empty it is safe to assume the folder doesn't exist and you can create it.

     

    Below is an example of that approach.

     

    1. Use an Send an HTTP request to SharePoint action. Use the following URI.

    _api/web/GetFolderByServerRelativeUrl('/sites/@{variables('SiteName')}/Lists/@{variables('ListName')}')/Folders?$filter=Name eq '@{variables('FolderName')}'

    I used a couple of variables, you can either replace those by your values or also recreate them in your flow setup.

     

    2. Add a Condition action. Check the length of the outputs. If that is equal to zero it means that no folder with that name is found.

    length(outputs('Send_an_HTTP_request_to_SharePoint')?['body']['value'])

     

    3. In the If Yes section add a Create new folder action.

     

    foldersfilter_httprequest.png

     

    Hopefully this helps to get you started with your setup.

  • syed_sme Profile Picture
    76 on at

    Hi @Expiscornovus , thanks for your suggestion. I followed your instructions and have configured my flow but how do I parse the folder name from the email body, to create the folder if it does not exist?

     

  • Expiscornovus Profile Picture
    33,189 Most Valuable Professional on at

    Hi @syed_sme ,

     

    Can you share an example email body?

  • syed_sme Profile Picture
    76 on at

    Hi @Expiscornovus , please see below.....

     

    Documents/Month End/Cadency/Automatic Reports

     

    This is all that is in the email body. Additional/new folders would be created in the Automatic Reports folder.

  • Expiscornovus Profile Picture
    33,189 Most Valuable Professional on at

    Hi @syed_sme,

     

    Thanks for the example body.

     

    Below is an example setup. In this example I am using a html to text action to convert the body property to text. The initialize variable action is using an indexof and some split functions to find the new folder name and store it in that variable.

     

    In this example I assuming the body of the e-mail will contain something like this:

    Documents/Month End/Cadency/Automatic Reports/NewFolderName

     

     

    if(equals(indexOf(outputs('Html_to_text')?['body'], 'Automatic Reports/'), -1), 'No New Folder', split(split(outputs('Html_to_text')?['body'], 'Automatic Reports/')[1], decodeUriComponent('%0A'))[0])

     

     

    split_foldername.png

     

    Btw, in case of that there isn't a new folder I am setting the value to No New Folder. You might want to use another condition to exclude these instances from the create new folder actions.

  • syed_sme Profile Picture
    76 on at

    Thanks @Expiscornovus ! 

     

    Making some good progress here.

     

    Had to make a change to the condition......

    syed_sme_1-1635430576398.png

    empty(outputs('Send_an_HTTP_request_to_SharePoint')?['body/d/results'])
    which seems to work but this is now creating folder called "No New Folder" rather than picking up the folder name.....
    syed_sme_2-1635437030156.png

     

     
  • Verified answer
    Expiscornovus Profile Picture
    33,189 Most Valuable Professional on at

    Hi @syed_sme,

     

    Great to hear that you made good progress.

     

    The folder name variable will sometimes have a value. The expression will give it the 'No New Folder' value when there is no sub folder.

     

    So, you can use the FolderName variable in a condition to check it's value (that it is not empty and doesn't have the 'No new folder' value)

     

    Below is an example of that approach.

     

    nonewfolder.png

  • syed_sme Profile Picture
    76 on at

    Thanks @Expiscornovus ,

     

    I finally managed to get this working following the approach you suggested. It took a while to get the conditions exactly as i wanted but it now works. 

     

    Here is the working logic.....

    syed_sme_0-1635761984618.png

     

    Thank you so much for your help and guidance with this. Very much appreciated. I've learned some new things to carry forward.

     

    Regards,

    Syed

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 538 Super User 2025 Season 2

#2
Tomac Profile Picture

Tomac 405 Moderator

#3
abm abm Profile Picture

abm abm 252 Most Valuable Professional

Last 30 days Overall leaderboard