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.
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)