Skip to content

Instantly share code, notes, and snippets.

@hansgafriedzal
Last active November 27, 2024 16:36
Show Gist options
  • Save hansgafriedzal/99fa0922ac049d25368947321b45993f to your computer and use it in GitHub Desktop.
Save hansgafriedzal/99fa0922ac049d25368947321b45993f to your computer and use it in GitHub Desktop.
Given a list of paths, remove all paths whose parent is already in the list.
<#
.SYNOPSIS
Given a list of paths, remove all paths whose parent is already in the list.
#>
function Get-PathCollapsed
{
param (
[string[]] $Paths
)
[System.Collections.ArrayList] $collapsedPaths = @()
$Paths | % { $p = $_
if ($collapsedPaths.Count -eq 0)
{
$collapsedPaths += $_
}
else
{
$isParent = $true
$itemToSwap = $null
$collapsedPaths | % {
if ($p -like "$_*")
{
$isParent = $false
}
elseif ($_ -like "$p*")
{
$itemToSwap = $_
}
}
if ($isParent)
{
$collapsedPaths += $p
}
if ($itemToSwap)
{
$collapsedPaths.Remove($itemToSwap)
}
}
}
return $collapsedPaths
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment