您现在的位置是:网站首页> 编程资料编程资料
PowerShell中把相对路径转换为绝对路径的2个方法_PowerShell_
2023-05-26
371人已围观
简介 PowerShell中把相对路径转换为绝对路径的2个方法_PowerShell_
在PowerShell中,有时候,我们需要把当前的相对路径解析为绝对路径,比如".\test.txt",我们想知道它的绝对路径的话,我们有两种方法可以实现。
1、有一个cmd-let,它叫Resolve-Path。
语法如下:
复制代码 代码如下:
Resolve-Path <相对路径>
如果指定的相对路径的文件或文件夹,不存在,则将提示如下:
复制代码 代码如下:
PS C:\Users\zhanghong> Resolve-Path .\test.txt
Resolve-Path : 找不到路径“C:\Users\zhanghong\test.txt”,因为该路径不存在。
所在位置 行:1 字符: 13
复制代码 代码如下:
+ Resolve-Path <<<< .\test.txt
+ CategoryInfo : ObjectNotFound: (C:\Users\zhanghong\test.txt:Str
ing) [Resolve-Path], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.Resol
vePathCommand
+ CategoryInfo : ObjectNotFound: (C:\Users\zhanghong\test.txt:Str
ing) [Resolve-Path], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.Resol
vePathCommand
如果位置存在,则提示找到的路径:
复制代码 代码如下:
PS C:\Users\zhanghong> Resolve-Path .\music
Path
----
C:\Users\zhanghong\music
Path
----
C:\Users\zhanghong\music
2、使用$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath方法
这个方法的好处是,不管这个相对路径的文件或文件夹存不存在,都可以顺利的它解析为绝对路径。
举例如下:
复制代码 代码如下:
PS C:\Users\zhanghong> $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('.\file.txt')
C:\Users\zhanghong\file.txt
C:\Users\zhanghong\file.txt
实际上,小编的这个C:\Users\zhanghong\file.txt文件是不存在的。
您可能感兴趣的文章:
相关内容
- PowerShell移动目录中指定文件的方法(非全部文件)_PowerShell_
- PowerShell命令中包含空格如何运行?_PowerShell_
- PowerShell脚本监控文件夹变化实例_PowerShell_
- Win8系统中使用PowerShell安装APPX应用命令介绍_PowerShell_
- PowerShell中使用通配符匹配文件路径的例子_PowerShell_
- PowerShell中按文件后缀过滤的实现代码_PowerShell_
- Powershell Profiles配置文件的存放位置介绍_PowerShell_
- PowerShell脚本中控制Windows DNS服务的方法_PowerShell_
- PowerShell替换字符串操作符replace简明教程_PowerShell_
- PowerShell获取字符串长度的方法_PowerShell_
