#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo: Remove Empty Folders Including Current Folder

In this post, we will be demonstrating how you can remove all empty folders from a specified folder. And then remove the current or specified folder if it does not contain and sub-folders or documents.

All of the ProjectWise related cmdlets are available using the PWPS_DAB module. At the time of this post, I am using version 23.2.0.0. Take a look at the help for each of the cmdlets to become familiar with their functionality, available parameters, etc.

  • Show-PWFolderBrowserDialog
  • Remove-PWEmptyFolders
  • Remove-PWFolder

POWERSHELL CODE SNIPPET

The following is a simple PowerShell code snippet to select a ProjectWise folder object, remove all empty folders contained in the selected folder, and then remove the selected folder if it does not contain any sub-folders or documents.

# Select folder to remove empty folders from and delete if contains no sub-folders or documents.
$pwFolder = Show-PWFolderBrowserDialog

# Determine if selected folder has any sub-folders.
if(($pwfolder.GetSubFolders()).Count -gt 0){
    # Remove all empty folders from selected folder.
    Remove-PWEmptyFolders -FolderPath $pwfolder.FullPath 

    <# Determine if the selected folder still has any sub-folders or documents.
        If yes, write warning. If no, remove the selected folder. #>
    if(($pwFolder.GetFolderDocuments()).Count -eq 0 -and ($pwFolder.GetSubFolders()).Count -eq 0){
        Remove-PWFolder -FolderPath $pwfolder.FullPath -ProceedWithDelete -RemoveFolders
    } else {
        Write-Warning -Message "$($pwFolder)"
    }
} # end if(($pwfolder.GetSubFolders()).Count -gt 0...

Experiment with it and have fun. Hopefully, you find this useful.

Please let me know if you have any questions or comments.  If you like this post, please click the Like button at the bottom of the page.

2 thoughts on “HowTo: Remove Empty Folders Including Current Folder”

  1. That’s a realy cool snippet. I would be allowed to use it, If in return it is possible to restore all deleted folder again by copy them with rights from our project template directory.

    We currently have given directories that user have to use for the Project. But after the end of the Project. The empty directories do not anymore have an Use.

    But there is also a case where dead Projects would be revived. In such cases we need the full directory structure again.

    Do you have a Snippet for copy the folder structure into a given project?

    Goal would be to:

    • restore the folderstructure with all empty folders.
    • have the access rights also copied
    • Replace the access rights with that from the source project also on all already available directory.

    This would solve two issues. At first missing directories and also old access rights.

    Like

    1. Hello Mkay.
      First, thank you for visiting my blog. I hope you find it useful.
      You should be able to find examples of what you need to accomplish spreadout within different posts on my blog. If you can’t find it here, I would recommend posting to the Bentley Communities site.
      Cheers,
      Brian

      Like

Leave a reply to Brian Flaherty Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.