Last active
January 24, 2018 14:28
-
-
Save marckassay/2f54ae68779c9f27fd130b193374335c to your computer and use it in GitHub Desktop.
Returns a valid path from a parent of one of its childs which overlaps that parent.
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 | |
Returns a valid path from a parent of one of its children which overlaps that parent's path. | |
.DESCRIPTION | |
In set-theory this will be considered a relative complement. That is the directories | |
in ChildPath that are not in Path. | |
A diagram to illustrate what is mentioned above: | |
A = C:\Windows\diagnostics\system | |
B = .\diagnostics\system\Keyboard\en-US\CL_LocalizationData.psd1 | |
B\A = .\Keyboard\en-US\CL_LocalizationData.psd1 | |
R = C:\Windows\diagnostics\system\Keyboard\en-US\CL_LocalizationData.psd1 | |
The path 'R' is what will be returned if -IsValid is not switched otherwise $true | |
will be returned. | |
.PARAMETER Path | |
Parent path of $ChildPath. This can be relative. | |
.PARAMETER ChildPath | |
Child path of $Path. | |
.PARAMETER IsValid | |
Returns true if function found a complement folder. False is returned if no complement was | |
found. | |
.EXAMPLE | |
Demonstration of using Get-MergedPath with truthly values | |
E:\Temp\AIT> Get-MergedPath E:\Temp\AIT\resources\ -ChildPath .\resources\android\AiT-Feature.jpg | |
E:\Temp\AIT\resources\android\AiT-Feature.jpg | |
E:\Temp\AIT> Get-MergedPath E:\Temp\AIT\resources\ -ChildPath .\resources\android\AiT-Feature.jpg -IsValid | |
True | |
.EXAMPLE | |
Demonstration of using Get-MergedPath with falsely values | |
E:\Temp\AIT> Get-MergedPath E:\Temp\AIT\resources\ -ChildPath .\reWWWources\android\AiT-Feature.jpg | |
E:\Temp\AIT> Get-MergedPath E:\Temp\AIT\resources\ -ChildPath .\reWWWources\android\AiT-Feature.jpg -IsValid | |
False | |
.LINK | |
https://gist.github.com/marckassay/2f54ae68779c9f27fd130b193374335c | |
#> | |
function Get-MergedPath { | |
[CmdletBinding()] | |
[OutputType([string])] | |
[OutputType([bool])] | |
Param | |
( | |
[Parameter(Mandatory = $true, | |
Position = 0, | |
ValueFromPipeline = $false)] | |
[ValidateNotNullOrEmpty()] | |
[string[]]$Path, | |
[Parameter(Mandatory = $true, | |
Position = 1, | |
ValueFromPipeline = $false)] | |
[ValidateNotNullOrEmpty()] | |
[string[]]$ChildPath, | |
[switch]$IsValid | |
) | |
$ParentBaseName = Get-Item $Path | Get-ItemPropertyValue -Name BaseName | |
$ChildBaseName = Split-Path -Path $ChildPath | |
if ($ChildBaseName.replace('\', '\\') -match $ParentBaseName ) { | |
if($IsValid.IsPresent) { | |
$true | |
} else { | |
Join-Path $Path -ChildPath $($ChildPath.Split($ParentBaseName)[1]) | |
} | |
} | |
else { | |
if($IsValid.IsPresent) { | |
$false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment