Last active
November 27, 2024 16:36
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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