0

When I use Set-Location (aka cd) to change the current directory in a PowerShell window, but deliberately avoid auto-complete and type the name in the "wrong" case...

PS C:\> Set-Location winDOWs

...then Get-Location (aka pwd) will return that "wrong" path name:

PS C:\winDOWs> Get-Location

Path
----
C:\winDOWs

This causes problems with 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

As you can see svn info fails when the user doesn't type the name of the working copy directory "myDir" with the correct letter case when cd'ing into it.

Is there a way to solve this? I could not find a suitable parameter of svn info.

Another option could be to overwrite PowerShell's cd alias and make sure the letter case of the typed path is fixed before actually cd'ing, but how to accomplish that? Resolve-Path, for example also returns the "wrong" directory name.

Good Night Nerd Pride
  • 8,245
  • 4
  • 49
  • 65
  • 1
    Does this answer your question http://stackoverflow.com/questions/7195337/how-do-i-get-a-path-with-the-correct-canonical-case-in-powershell – Shmukko Feb 06 '17 at 15:26
  • http://get-carbon.org/Resolve-PathCase.html – gvee Feb 06 '17 at 16:09

1 Answers1

1

Something like this might work for you:

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 
}

This will give you the proper casing for the "System32" portion of the path. You will need to recursively call this piece of code for all pieces of the path, e.g. C:\Windows, C:\Windows\System32, etc.

Final recursive function

Here you go:

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

}

Example:

Set-Location (Get-CaseSensitivePath C:\winDOWs\sysTEm32)
Nasir
  • 10,935
  • 8
  • 31
  • 39
  • Awesome. A pure PowerShell solution that seems to be even faster than [Resolve-PathCase from the Carbon PowerShell module](http://stackoverflow.com/a/9185476/1025555). – Good Night Nerd Pride Feb 07 '17 at 15:39