src/utility/Get-MergedPath.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<#
.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
        }
    }
}