Repository navigation
Expand file tree
/
Copy pathGetRelativePath.ps1
More file actions
55 lines (50 loc) · 2.57 KB
/
Copy pathGetRelativePath.ps1
File metadata and controls
55 lines (50 loc) · 2.57 KB
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
function GetRelativePath {
<#
.SYNOPSIS
Returns the relative path, or $Path if the paths don't share the same root.
For backward compatibility, this is [System.IO.Path]::GetRelativePath for .NET 4.x
#>
[OutputType([string])]
[CmdletBinding()]
param(
# The source path the result should be relative to. This path is always considered to be a directory.
[Parameter(Mandatory)]
[string]$RelativeTo,
# The destination path.
[Parameter(Mandatory)]
[string]$Path
)
# This giant mess is because PowerShell drives aren't valid filesystem drives
$Drive = $Path -replace "^([^\\/]+:[\\/])?.*", '$1'
if ($Drive -ne ($RelativeTo -replace "^([^\\/]+:[\\/])?.*", '$1')) {
Write-Verbose "Paths on different drives"
return $Path # no commonality, different drive letters on windows
}
$RelativeTo = $RelativeTo -replace "^[^\\/]+:[\\/]", [IO.Path]::DirectorySeparatorChar
$Path = $Path -replace "^[^\\/]+:[\\/]", [IO.Path]::DirectorySeparatorChar
$RelativeTo = [IO.Path]::GetFullPath($RelativeTo).TrimEnd('\/') -replace "^[^\\/]+:[\\/]", [IO.Path]::DirectorySeparatorChar
$Path = [IO.Path]::GetFullPath($Path) -replace "^[^\\/]+:[\\/]", [IO.Path]::DirectorySeparatorChar
$commonLength = 0
while ($Path[$commonLength] -eq $RelativeTo[$commonLength]) {
$commonLength++
}
if ($commonLength -eq $RelativeTo.Length -and $RelativeTo.Length -eq $Path.Length) {
Write-Verbose "Equal Paths"
return "." # The same paths
}
if ($commonLength -eq 0) {
Write-Verbose "Paths on different drives?"
return $Drive + $Path # no commonality, different drive letters on windows
}
Write-Verbose "Common base: $commonLength $($RelativeTo.Substring(0,$commonLength))"
# In case we matched PART of a name, like C:\Users\Joel and C:\Users\Joe
while ($commonLength -gt $RelativeTo.Length -and ($RelativeTo[$commonLength] -ne [IO.Path]::DirectorySeparatorChar)) {
$commonLength--
}
Write-Verbose "Common base: $commonLength $($RelativeTo.Substring(0,$commonLength))"
# create '..' segments for segments past the common on the "$RelativeTo" path
if ($commonLength -lt $RelativeTo.Length) {
$result = @('..') * @($RelativeTo.Substring($commonLength).Split([IO.Path]::DirectorySeparatorChar).Where{ $_ }).Length -join ([IO.Path]::DirectorySeparatorChar)
}
(@($result, $Path.Substring($commonLength).TrimStart([IO.Path]::DirectorySeparatorChar)).Where{ $_ } -join ([IO.Path]::DirectorySeparatorChar))
}