Skip to content

Instantly share code, notes, and snippets.

@PsPsam
Last active May 31, 2017 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PsPsam/7e4b7862379a087fb840bd545537a703 to your computer and use it in GitHub Desktop.
Save PsPsam/7e4b7862379a087fb840bd545537a703 to your computer and use it in GitHub Desktop.
Remove empty folders
function remove-emptyfolders-t1 {
Param
(
# Path to the folder to check for old files
[Parameter(Mandatory, HelpMessage = 'Will remove empty subfolders',
ValueFromPipelineByPropertyName,
Position = 0)]
[string]$path
)
Get-ChildItem -Path $path -Recurse -Force -Directory |
Where-Object {$null -eq (Get-ChildItem -Path $_.FullName -File -Recurse -Force)} |
Remove-Item -Force -Recurse
}
function remove-emptyfolders-t2 {
Param
(
# Path to the folder to check for old files
[Parameter(Mandatory, HelpMessage = 'Will remove empty subfolders',
ValueFromPipelineByPropertyName,
Position = 0)]
[string]$path
)
$test = Get-ChildItem -Path $path -Recurse -Force -Directory |
Where-Object {$null -eq (Get-ChildItem -Path $_.FullName -File -Recurse -Force)}
$test | Remove-Item -Force -Recurse
}
Describe "Remove-EmptyFolders-t1" {
Context "path has empty folders and non empty folders" {
Setup -Dir "env\empty\full"
Setup -Dir "env\empty\diff"
Setup -Dir "env\empty\log"
Setup -Dir "env\notempty\full"
Setup -Dir "env\notempty\diff"
Setup -Dir "env\notempty\log"
Setup -file "env\notempty\full\file.bak"
Setup -file "env\notempty\diff\file.bak"
Setup -file "env\notempty\log\file.bak"
Setup -Dir "env\half\full"
Setup -Dir "env\half\diff"
Setup -Dir "env\half\log"
Setup -file "env\half\full\file.bak"
Remove-EmptyFolders-t1 -path "TestDrive:\env"
It "removes the folder" {
Test-Path "TestDrive:\env\empty" | Should Be $false
}
It "does not removes the folder notempty" {
Test-Path "TestDrive:\env\notempty" | Should Be $true
}
}
}
Describe "Remove-EmptyFolders-t2" {
Context "path has empty folders and non empty folders" {
Setup -Dir "env\empty\full"
Setup -Dir "env\empty\diff"
Setup -Dir "env\empty\log"
Setup -Dir "env\notempty\full"
Setup -Dir "env\notempty\diff"
Setup -Dir "env\notempty\log"
Setup -file "env\notempty\full\file.bak"
Setup -file "env\notempty\diff\file.bak"
Setup -file "env\notempty\log\file.bak"
Setup -Dir "env\half\full"
Setup -Dir "env\half\diff"
Setup -Dir "env\half\log"
Setup -file "env\half\full\file.bak"
Remove-EmptyFolders-t2 -path "TestDrive:\env"
It "removes the folder" {
Test-Path "TestDrive:\env\empty" | Should Be $false
}
It "does not removes the folder notempty" {
Test-Path "TestDrive:\env\notempty" | Should Be $true
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment