Skip to content

Instantly share code, notes, and snippets.

@teejaded
Created March 2, 2017 03:10
Show Gist options
  • Save teejaded/07ed5cb167bc8843fd636563a43e9241 to your computer and use it in GitHub Desktop.
Save teejaded/07ed5cb167bc8843fd636563a43e9241 to your computer and use it in GitHub Desktop.
function Find-EmptyDirs {
param(
[parameter(Mandatory = $true, Position = 0)]
[ValidateScript({ Test-Path $_ })]
[string] $Path,
[parameter(Mandatory = $false, Position = 1)]
[switch] $Hidden,
[parameter(Mandatory = $false, Position = 2)]
[array] $Exclude
)
$Path = $Path.TrimEnd('\')
$empty = @{}
Get-ChildItem @PSBoundParameters -Recurse -Directory | %{$empty[$_.FullName] = $true}
Get-ChildItem @PSBoundParameters -Recurse -File -Exclude "desktop.ini" | ForEach-Object {
for($d = $_.Directory; $d.FullName -ine $Path; $d = $d.Parent) {
if($empty[$d.FullName] -eq $false) { break }
$empty[$d.FullName] = $false
}
}
return $empty.Keys | Where-Object { $empty[$_] }
}
# Create test folder structure
function Make-Tree {
param($tmpdir,$count=1000)
$dirs = New-Object System.Collections.ArrayList($count+1)
$dirs.Add($tmpdir) | Out-Null
1..$count | %{
$dir = $dirs[(Get-Random -Maximum $dirs.count -Minimum 0)]
$path = Join-Path -path $dir -childpath (Get-Random -Maximum 99999999 -Minimum 1000000).ToString()
mkdir -path $path | Out-Null
$dirs.Add($path) | Out-Null
}
1..$count | %{
$dir = $dirs[(Get-Random -Maximum $dirs.count -Minimum 0)]
$filename = "$((Get-Random -Maximum 99999999 -Minimum 1000000).ToString()).dat"
$file = Join-Path -path $dir -childpath $filename
Set-Content -Path $file -Value ""
}
}
# Check that a test is true or output the test
function assert {
param($test)
if($test -eq $true) { return }
$stack = Get-PSCallStack
$line = ($stack[0].InvocationInfo.Line -replace 'assert','').Trim()
$num = ($stack[0].ScriptLineNumber)
throw "Assertion failed $line at line $num"
}
# Test Find-EmptyDirs
function Test-EmptyDirs {
param(
[parameter(Mandatory=$true,Position=0)] $tmpdir
)
if ((dir $tmpdir).count -gt 0) {
throw "$tmpdir is not empty. Please specify an empty path."
}
#fix tmpdir case
$tmpdir = (Get-Item $tmpdir).FullName
# Make some test files
mkdir "$tmpdir\empty" | Out-Null
mkdir "$tmpdir\empty\sub"| Out-Null
mkdir "$tmpdir\full"| Out-Null
mkdir "$tmpdir\fullsub"| Out-Null
mkdir "$tmpdir\fullsub\sub"| Out-Null
sc -Path "$tmpdir\full\file.txt" -Value ""
sc -Path "$tmpdir\fullsub\sub\file.txt" -Value ""
$res = Find-EmptyDirs -Path $tmpdir
assert $res.Contains("$tmpdir\empty")
assert $res.Contains("$tmpdir\empty\sub")
assert !$res.Contains("$tmpdir\full")
assert !$res.Contains("$tmpdir\fullsub")
assert !$res.Contains("$tmpdir\fullsub\sub")
assert !$res.Contains("$tmpdir\full\file.txt")
assert !$res.Contains("$tmpdir\fullsub\sub\file.txt")
# Do random tests
1..100 | %{
Make-Tree $tmpdir -count 100
$dirs = Find-EmptyDirs -Path $tmpdir
$failed = $dirs | ?{ (Get-ChildItem -Path $_ -Recurse -File).count -gt 0 }
assert ($failed.count -le 0)
del -Path "$tmpdir\*" -Force -Recurse
Write-Host "Completed $_ iterations"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment