如何在PowerShell中将绝对路径转换为相对路径?
问题描述:
我想在 PowerShell 脚本中将路径转换为相对路径.如何使用 PowerShell 执行此操作?
I'd like to convert a path to a relative path in a PowerShell script. How do I do this using PowerShell?
例如:
Path to convert: c:\documents\mynicefiles\afile.txt
Reference path: c:\documents
Result: mynicefiles\afile.txt
和
Path to convert: c:\documents\myproject1\afile.txt
Reference path: c:\documents\myproject2
Result: ..\myproject1\afile.txt
答
我发现了一些内置的东西,解析路径:
I found something built in, Resolve-Path:
Resolve-Path -Relative
这将返回相对于当前位置的路径.一个简单的用法:
This returns the path relative to the current location. A simple usage:
$root = "C:\Users\Dave\"
$current = "C:\Users\Dave\Documents\"
$tmp = Get-Location
Set-Location $root
Resolve-Path -relative $current
Set-Location $tmp