如何在PowerShell中将绝对路径转换为相对路径? [英] How to convert absolute path to relative path in PowerShell?

查看:40
本文介绍了如何在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

这篇关于如何在PowerShell中将绝对路径转换为相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆