是否有与 [System.IO.Path]::GetFullPath($fileName); 等效的 powershell cmdlet当 $fileName 不存在时?
如果 $fileName 存在,则相当于 [ System.IO.Path]::GetFullPath($fileName);
是 (Get-Item $fileName).FullName
。但是,如果路径不存在,则会引发异常。他们是我缺少的另一个 cmdlet 吗?
Join-Path
不可接受,因为它当传递绝对路径时将不起作用:
C:\Users\zippy\Documents\deleteme> join-path $pwd 'c:\config.sys'
C:\Users\zippy\Documents\deleteme\c:\config.sys
C:\Users\zippy\Documents\deleteme>
If $fileName exists then the cmdlet equivalent of [System.IO.Path]::GetFullPath($fileName);
is (Get-Item $fileName).FullName
. However, an exception is thrown if the path does not exist. Is their another cmdlet I am missing?
Join-Path
is not acceptable because it will not work when an absolute path is passed:
C:\Users\zippy\Documents\deleteme> join-path $pwd 'c:\config.sys'
C:\Users\zippy\Documents\deleteme\c:\config.sys
C:\Users\zippy\Documents\deleteme>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信,
Join-Path
将是获取不存在项目的路径的方法。像这样的事情:更新:
我不明白为什么你不想使用.Net“代码”。 Powershell是基于.Net的。所有 cmdlet 都是 .Net 代码。避免它的唯一有效原因是,当您使用 .Net Code 时,当前目录是启动 Powershell 的目录,而不是
$pwd
我只是列出了我认为可以做到这一点的方法您可以处理绝对路径和相对路径。它们看起来都不比 GetFullPath() 更简单:
如果您担心是否传递了绝对路径,您可以执行以下操作:
这是一个丑陋的
问题类似的问题,具有类似的答案:< a href="https://stackoverflow.com/q/3038337/526535">Powershell:解析可能不存在的路径?
Join-Path
would be the way to get a path for a non-existant item I believe. Something like this:Update:
I don't understand why you don't want to use .Net "code". Powershell is .Net based. All cmdlets are .Net code. Only valid reason for avoiding it is that when you use .Net Code, the current directory is the directory from which Powershell was started and not
$pwd
I am just listing the ways I believe this can be done so that you can handle absolute and relateive paths. None of them seem simpler than the
GetFullPath()
one:If you are worried if absolute path is passed or not, you can do something like:
This is the ugly one
Similar question, with similar answers: Powershell: resolve path that might not exist?
您可以在获取全名之前使用 Test-Path cmdlet 检查它是否存在。
编辑:
刚刚看到您上面关于 Test-Path 相当于 [system.io.file]::exists() 函数的评论,我相信我现在更好地理解您的问题了。
我认为答案是否定的,但您可以自己制作。
您可以通过使参数接受管道(字符串和属性)来整理它,但它达到了目的。
You could use the Test-Path cmdlet to check it exists before getting the fullname.
EDIT:
Just seen your comment above about Test-Path being the equivalent to the [system.io.file]::exists() function and I believe I understand your question better now.
No is the answer as I see it but you could make your own.
you could tidy it up some by making the parameters accept pipeline, both strings and properties but it serves the purpose.
您可以删除“c:\config.sys”的驱动器限定符(使用分割路径),然后连接两个路径:
You can remove the drive qualifier of 'c:\config.sys' (Using Split-Path) and then join the two paths: