Functions/Resolve-PathCase.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
86
87
88
89
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function Resolve-PathCase
{
    <#
    .SYNOPSIS
    Returns the real, canonical case of a path.
     
    .DESCRIPTION
    The .NET and Windows path/file system APIs respect and preserve the case of paths passed to them. This function will return the actual case of a path on the file system, regardless of the case of the string passed in.
     
    If the path doesn't an exist, an error is written and nothing is returned.
 
    .EXAMPLE
    Resolve-PathCase -Path "C:\WINDOWS\SYSTEM32"
     
    Returns `C:\Windows\system32`.
     
    .EXAMPLE
    Resolve-PathCase -Path 'c:\projects\carbon'
     
    Returns `C:\Projects\Carbon`.
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
        [string]
        # The path whose real, canonical case should be returned.
        [Alias('FullName')]
        $Path
    )
    
    Set-StrictMode -Version 'Latest'

    Use-CallerPreference -Cmdlet $PSCmdlet -Session $ExecutionContext.SessionState

    if( -not (Test-Path -Path $Path) )
    {
        Write-Error "Path '$Path' not found."
        return
    }

    $uri = [uri]$Path
    if( $uri.IsUnc )
    {
        Write-Error ('Path ''{0}'' is a UNC path, which is not supported.' -f $Path)
        return
    }

    if( -not ([IO.Path]::IsPathRooted($Path)) )
    {
        $Path = (Resolve-Path -Path $Path).Path
    }
    
    $qualifier = '{0}\' -f (Split-Path -Qualifier -Path $Path)
    $qualifier = Get-Item -Path $qualifier | Select-Object -ExpandProperty 'Name'
    $canonicalPath = ''
    do
    {
        $parent = Split-Path -Parent -Path $Path
        $leaf = Split-Path -Leaf -Path $Path
        $canonicalLeaf = Get-ChildItem -Path $parent -Filter $leaf
        if( $canonicalPath )
        {
            $canonicalPath = Join-Path -Path $canonicalLeaf -ChildPath $canonicalPath
        }
        else
        {
            $canonicalPath = $canonicalLeaf
        }
    }
    while( $parent -ne $qualifier -and ($Path = Split-Path -Parent -Path $Path) )

    return Join-Path -Path $qualifier -ChildPath $canonicalPath
}

Set-Alias -Name 'Get-PathCanonicalCase' -Value 'Resolve-PathCase'