【问题标题】:Remove the last extension from path从路径中删除最后一个扩展名
【发布时间】:2019-12-01 00:41:45
【问题描述】:

通过使用 (get-command Oscdimg).Path ,我能够检索完整路径。

这是我得到的:D:\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\oscdimg.exe

我真正想要的是:D:\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg

我不希望 oscdimg.exe 在我的路径中。有什么方法可以去除吗?

【问题讨论】:

  • 你的意思是Get-ChildItem吗?在这种情况下,您可以执行 $DirectoryName = (Get-ChildItem 'D:\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\oscdimg.exe').DirectoryName
  • @phiz 是的,它有效。非常感谢。

标签: powershell


【解决方案1】:

根据@phiz 评论

$filename = "D:\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\oscdimg.exe"
$file = Get-ChildItem $filename
$file.DirectoryName

输出

D:\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg

【讨论】:

    【解决方案2】:

    有一个 cmdlet 可以满足您的要求。 [咧嘴一笑] 看...

    $FilePath = 'D:\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\oscdimg.exe'
    
    Split-Path -Path $FilePath -Parent
    

    输出...

    D:\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg
    

    【讨论】:

      【解决方案3】:

      您是否尝试过“替换”运算符? 前任。 输入:

      'D:\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\oscdimg.exe' -replace '.exe',''
      

      输出:

      'D:\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\amd64\Oscdimg\oscdimg'
      

      【讨论】:

      • 嗯,我想要的是从我的路径中完全删除 oscdimg.exe。不仅仅是.exe这个词。但我得到了答案,你可以从上面参考。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 2011-04-09
      相关资源
      最近更新 更多