Sharepoint online - trying to hide all empty directories in a view

Copper Contributor

My goal: 
Hide empty directories and only show directories if there is a file or files stored in the sub directories anywhere. 

 

Scenario: 

I have a document library with many top level directories. Inside these directories are the same set of predefined directories, which may or may not be empty. Inside these predefined directories, there may be additional directories or files (or may be empty). 

To save user strain of finding info, I'd like to customise a view to not show empty directories unless there is at least one file within the tree. I've been unsuccessful at doing this which is my problem.

 

Folder structure:
Doc library:
--Folder1
----Fol1Sub1
----Fol1Sub2
------Fol1Sub2-*file here*
  Folder2
----Fol2Sub1
----Fol2Sub2
  Folder3
----Fol3Sub1
----Fol3Sub2
  Folder4
----Fol4Sub1

 

There is a file in subfolder 2 of top folder 1. 


What I've tried:

I have tried to add columns that are prebuilt in sharepoint called "folder child count" and "item child count". My issue with this is that these columns do not give the desired output as they only show whats beneath the current view. Ie, item count will show how many items in that directory. Using the above folder layout, "Folder1" would show item count as ZERO because it does not drill down to where the file is located. 

If I could have a column that shows item child count as larger than 0 when there is a file nested beneath, then I could modify the view to exclude directories with item child count = 0. But, since this column will show 0 when there is indeed a file below, this is not a useful plan.

 

I've tried to take a look at pnp search with the idea to create a custom column where I can pull in data and calculate the item child count but I've only been exposed to sharepoint for a couple of weeks so I'm a bit confused how to go about this. 

 

I did find some links to scripts [1] [2] that apparently can do what I need but I dont have the context to understand where to apply this code or whether it's meant to be used from powershell only (cli only is not usefuel, either). 

 

[1]Count of Files Under a Folder in a Document Library - SharePoint Stack Exchange

[2]sharepoint online - How to get count of files Inside a subfolder- pnp-js? - SharePoint Stack Exchang...

 

2 Replies

@tonyp395  Create a view and add Item Child Count , Folder Child Count - make sure those values are what you expect.   Then modify your view so it shows where Item Child Count or Folder Child Count are greater than 0.   

PamDeGraffenreid_0-1654034194085.png

 

@PamDeGraffenre

 

He's already addressed why using this solution doesn't work. It doesn't account for the count of subfolders. Are there any other solutions?