Author Details

avatar-teacher

Evolvous

We are a team of tech enthusiast and this section is where we love to share our experience and our knowledge. We try our best to help you understand technical topics by giving you live instances of how-to’s, guides, tips, FAQ’s and troubleshoots.

If you like it or have questions, do share your feedback in the comments section below.

Documents count of SharePoint folder if it exists using Power Automate

In this blog, we will see how we can automate process of counting files present in SharePoint folder. We have one SharePoint list named ‘Employee’ with columns First name, Last name, and Document Count. SharePoint Library ‘Documents’ has folders followed name patterned as ‘First name Last name’ from Employees list. Now our requirement is to run a flow to get number of files/documents uploaded in each folder of Documents library.

Initialize variables and get items of SharePoint list:

Let’s start with manual trigger action in the flow. Initialize four variables as shown in following screenshot.

Initialize variables and get items of SharePoint list - Evolvous

Next step is to get all rows from Employee list.

Now for each item of Employee list we need to extract full name from First name and Last name column, then check whether the folder with that name exists in Document library or not. Next step is to use Apply to each action to iterate through all the items of list. Inside apply to each action set variables as follows. Please see following screenshot.

Where Fullname set to concat(items(‘Apply_to_each’)?[‘FirstName’],’ ‘,items(‘Apply_to_each’)?[‘LastName’])

Check whether a folder exists in SharePoint Library:

Next, we need to use the SharePoint List Folder action. This action returns files contained in a SharePoint folder. Here I am using the FolderName variable from the previous step under the file identifier setting as dynamic encodeURIComponent expression (to remove any spaces or special characters). For example: encodeUriComponent(variables(‘folderPath’)).

Where File Identifier set to encodeUriComponent(variables(‘FolderName’)). For the above List Folder action set the configure run after property as follows by clicking on three dots.

Documents count of SharePoint folder if it exists using Power Automate

After that add parallel branch step and add two actions under parallel branch.

  • Set IsFolderExist variable to true.

Set configures run after property of above action as follows:

Documents count of SharePoint folder if it exists using Power Automate

Set IsFolderExist variable to false.

Set configures run after property of above action as follows:

List Folder action with parallel branching will looks like as follows:

Documents count of SharePoint folder if it exists using Power Automate

If List Folder action succeeded, then left side of parallel branch executes and it will set IsFolderExist to true else right side of parallel branch executes and it will set ISFolderEsixt to false.

The next step is to check of whether SharePoint folder found or not using condition control.

Set configures run after property of above action as follows:

Condition for sharepoint - Evolvous

Calculate files count of the folder and update list:

In this section we will see how to get files count from the folder. On the yes side of the condition, I have used Get files action and used Filter Array action to list only files not subfolders within folder. Final action is set FilesCount variable to length of Filter Array action.

If you want all files/subfolders count of the folder, then skip filter array action and to set the FilesCount variables use length of Get files array.

On No side of the condition set FilesCount variable to 0 as it executes folder not found part of the flow.

Now I am using update SharePoint list action to set documents count field of employees list as follows:

Documents count of SharePoint folder if it exists using Power Automate

Following is my flow run result for folder exist ->

Documents count of SharePoint folder if it exists using Power Automate

Following flow run result is for folder not exist ->

Documents count of SharePoint folder if it exists using Power Automate

In this way you can check whether SharePoint folder exist or not and further grab content of folder to perform your next steps as per your requirements.

I hope you found this blog helpful.