是否有与 [System.IO.Path]::GetFullPath($fileName); 等效的 powershell cmdlet当 $fileName 不存在时?

发布于 2024-11-30 23:39:30 字数 682 浏览 0 评论 0原文

如果 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一张白纸 2024-12-07 23:39:30

我相信,Join-Path 将是获取不存在项目的路径的方法。像这样的事情:

join-path $pwd $filename

更新:

我不明白为什么你不想使用.Net“代码”。 Powershell是基于.Net的。所有 cmdlet 都是 .Net 代码。避免它的唯一有效原因是,当您使用 .Net Code 时,当前目录是启动 Powershell 的目录,而不是 $pwd

我只是列出了我认为可以做到这一点的方法您可以处理绝对路径和相对路径。它们看起来都不比 GetFullPath() 更简单:

$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($filename)

如果您担心是否传递了绝对路径,您可以执行以下操作:

if(Split-Path $filename -IsAbsolute){
    $filename
}
else{
    join-path $pwd $filename  # or join-path $pwd (Split-Path -Leaf $filename)
}

这是一个丑陋的

 $item = Get-Item $filename -ea silentlycontinue
 if (!$item) {
 $error[0].targetobject
 } else{
 $item.fullname
 }

问题类似的问题,具有类似的答案:< 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:

join-path $pwd $filename

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:

$ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($filename)

If you are worried if absolute path is passed or not, you can do something like:

if(Split-Path $filename -IsAbsolute){
    $filename
}
else{
    join-path $pwd $filename  # or join-path $pwd (Split-Path -Leaf $filename)
}

This is the ugly one

 $item = Get-Item $filename -ea silentlycontinue
 if (!$item) {
 $error[0].targetobject
 } else{
 $item.fullname
 }

Similar question, with similar answers: Powershell: resolve path that might not exist?

油饼 2024-12-07 23:39:30

您可以在获取全名之前使用 Test-Path cmdlet 检查它是否存在。

if (Test-Path $filename) {(Get-Item $fileName).FullName}

编辑:

刚刚看到您上面关于 Test-Path 相当于 [system.io.file]::exists() 函数的评论,我相信我现在更好地理解您的问题了。

我认为答案是否定的,但您可以自己制作。

function Get-Fullname {
  param($filename)
  process{
      if (Test-Path $filename) {(Get-Item $fileName).FullName} 
  }
}

您可以通过使参数接受管道(字符串和属性)来整理它,但它达到了目的。

You could use the Test-Path cmdlet to check it exists before getting the fullname.

if (Test-Path $filename) {(Get-Item $fileName).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.

function Get-Fullname {
  param($filename)
  process{
      if (Test-Path $filename) {(Get-Item $fileName).FullName} 
  }
}

you could tidy it up some by making the parameters accept pipeline, both strings and properties but it serves the purpose.

枫林﹌晚霞¤ 2024-12-07 23:39:30

您可以删除“c:\config.sys”的驱动器限定符(使用分割路径),然后连接两个路径:

PS > Join-Path $pwd (Split-Path 'c:\config.sys' -NoQualifier)    
C:\Users\zippy\Documents\deleteme\config.sys

You can remove the drive qualifier of 'c:\config.sys' (Using Split-Path) and then join the two paths:

PS > Join-Path $pwd (Split-Path 'c:\config.sys' -NoQualifier)    
C:\Users\zippy\Documents\deleteme\config.sys
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文