我想在PowerShell脚本中将路径转换为相对路径.如何使用PowerShell执行此操作?
例如:
Path to convert: c:\documents\mynicefiles\afile.txt
Reference path: c:\documents
Result: mynicefiles\afile.txt
Run Code Online (Sandbox Code Playgroud)
和
Path to convert: c:\documents\myproject1\afile.txt
Reference path: c:\documents\myproject2
Result: ..\myproject1\afile.txt
Run Code Online (Sandbox Code Playgroud)
Dav*_*ier 54
我找到了内置的东西,Resolve-Path:
Resolve-Path -Relative
Run Code Online (Sandbox Code Playgroud)
这将返回相对于当前位置的路径.一个简单的用法:
$root = "C:\Users\Dave\"
$current = "C:\Users\Dave\Documents\"
$tmp = Get-Location
Set-Location $root
Resolve-Path -relative $current
Set-Location $tmp
Run Code Online (Sandbox Code Playgroud)
使用内置System.IO.Path.GetRelativePath比接受的答案更简单:
[System.IO.Path]::GetRelativePath($relativeTo, $path)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17723 次 |
| 最近记录: |