I got a question on how to clean a document library for empty folders. I thought i would like to share the code with you.
It’s a pretty simple way to do it. I the following list cleaning work for SharePoint 2007. Feel free to use the code and come with suggestions.
/// <summary> /// Cleans the specific folder. /// </summary>
///The root folder.
private static void CleanSpecificFolder(SPFolder rootFolder)
{
foreach (SPFolder folder in rootFolder.SubFolders)
{
CleanSpecificFolder(folder);
}
if( rootFolder.SubFolders.Count == 0 && rootFolder.Files.Count == 0)
{
rootFolder.Delete();
}
}
This function should be called with the following code, where you activate the cleaning of empty folders on the list
foreach (SPFolder subFolder in spList.RootFolder.SubFolders)
{
CleanSpecificFolder(subFolder);
}
The function is made recursive. The function is actived a foreach loop, as we don’t want to delete the rootfolder on the SPList item, since that would mess the list up.
I hope you got inspired by this post.