0

当我使用Set-Location(aka cd) 在 PowerShell 窗口中更改当前目录,但故意避免自动完成并在“错误”情况下键入名称时......

PS C:\> Set-Location winDOWs

...然后Get-Location(又名pwd)将返回那个“错误”的路径名:

PS C:\winDOWs> Get-Location

Path
----
C:\winDOWs

这会导致以下问题svn info

PS C:\svn\myDir> svn info --show-item last-changed-revision
2168
PS C:\svn\myDir> cd ..\MYDIR
PS C:\svn\MYDIR> svn info --show-item last-changed-revision
svn: warning: W155010: The node 'C:\svn\MYDIR' was not found.

svn: E200009: Could not display info for all targets because some targets don't exist

正如您所看到svn info的,当用户输入工作副本目录“myDir”的名称时没有输入正确的字母大小写时会失败cd

有没有办法解决这个问题?我找不到合适的参数svn info

另一种选择可能是覆盖 PowerShell 的cd别名并确保键入路径的字母大小写在实际cd'ing 之前已修复,但如何实现呢?Resolve-Path,例如还返回“错误”的目录名称。

4

1 回答 1

1

这样的事情可能对你有用:

Set-Location C:\winDOWs\sysTEm32    
$currentLocation = (Get-Location).Path
$folder = Split-Path $currentLocation -Leaf
$casedPath = ([System.IO.DirectoryInfo]$currentLocation).Parent.GetFileSystemInfos($folder).FullName

# if original path and new path are equal (case insensitive) but are different with case-sensitivity. cd to new path.
if($currentLocation -ieq $casedPath -and $currentLocation -cne $casedPath)
{
    Set-Location -LiteralPath $casedPath 
}

这将为路径的“System32”部分提供正确的大小写。您将需要为路径的所有部分递归调用这段代码,例如 C:\Windows、C:\Windows\System32 等。

最终递归函数

干得好:

function Get-CaseSensitivePath
{
    param([System.IO.DirectoryInfo]$currentPath)

    $parent = ([System.IO.DirectoryInfo]$currentPath).Parent
    if($null -eq $parent)
    {
        return $currentPath.Name
    }

    return Join-Path (Get-CaseSensitivePath $parent) $parent.GetDirectories($currentPath.Name).Name

}

例子:

Set-Location (Get-CaseSensitivePath C:\winDOWs\sysTEm32)
于 2017-02-07T00:06:37.583 回答