powershell-2.0 - 如何在 PowerShell 中获取没有文件名的文件路径?

标签 powershell-2.0

我在名为 $file 的变量中有 C:\Temp\MyFolder\mytextfile.txt

我想在另一个变量中使用 C:\Temp\MyFolder\

最佳答案

这很简单 您可以使用这段代码来做到这一点:

  • 如果您想从 $file 变量中提取文件路径:

    $file = 'C:\Temp\MyFolder\mytextfile.txt'
    
    $myFilePath = Split-Path $file -Parent
    
    $myFilePath = $myFilePath+'\'
    
    Write-Host $myFilePath
    
  • 如果你只想提取文件名,

    $file = 'C:\Temp\MyFolder\mytextfile.txt'
    
    $myFileName = Split-Path $file -leaf
    
    Write-Host $myFileName
    

我知道这是一个老问题,但仍然, 如果您觉得此答案有帮助,请将其标记为已接受,谢谢编码愉快:)

关于powershell-2.0 - 如何在 PowerShell 中获取没有文件名的文件路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64710689/

相关文章:

powershell - 如何在哈希表的末尾添加键/值对?

Powershell v2::加载 COM 互操作 DLL

windows - 以编程方式为 USB 设备安装 "Incompatible"驱动程序

windows - 在 PowerShell 中使用 sudo 和 SSH-Session

powershell - 为什么 Get-content 支持 -wait 参数?

powershell - PowerShell:控制台应用程序的远程执行失败

c# - 在 powershell 和 C# 之间共享变量

powershell-2.0 - PowerShell 远程调用神秘挂起

powershell - Powershell中的Out-File -append不会产生新行,并且会将字符串分成字符

linux - windows 上的远程 powershell 脚本不通过 linux 中的 python 脚本运行