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.
Let’s start with manual trigger action in the flow. Initialize four variables as shown in following screenshot.

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’])
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.

After that add parallel branch step and add two actions under parallel branch.
Set configures run after property of above action as follows:

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:

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:

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:

Following is my flow run result for folder exist ->

Following flow run result is for folder not exist ->

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.
=====
Plus read