How do I get a path with the correct (canonical) case in PowerShell?

后端 未结 4 624
长情又很酷
长情又很酷 2021-01-18 11:34

I have a script that accepts a directory as an argument from the user. I\'d like to display the name of the directory path as it is displayed in Windows. I.e.,

<         


        
4条回答
  •  失恋的感觉
    2021-01-18 12:19

    Using Christian's GetDirectories suggestion, here's another solution that's not quite as involved:

    function Get-PathCanonicalCase
    {
        param( $path )
    
        $newPath = (Resolve-Path $path).Path
        $root = [System.IO.Path]::GetPathRoot( $newPath )
        if ( $newPath -ne $root ) # Handle case where changing to root directory
            { $newPath = [System.IO.Directory]::GetDirectories( $root, $newPath.Substring( $root.Length ) )[ 0 ] }
        $newPath
    }
    

    EDIT: Thanks for all the help.

    Btw, all I wanted this for was to use in a little utility script overriding the default cd alias, allowing me to specify some 'root' directories that are searched if the path doesn't exist relative to the current directory. I.e., it allows me to cd Documents, cd trunk, cd Release-10.4 regardless of my current location. And it annoyed me to have the prompt in the case that I entered it, instead of its actual case.

    # Usage: 
    # Set up in $profile - define the functions and reassign 'cd'.  Example:
    # -----
    #  . .\Set-LocationEx.ps1 "c:\dev\Code", "c:\dev\Code\releases", "$HOME" -Verbose
    # if (test-path alias:cd) { remove-item alias:cd > $null }
    # Set-Alias cd Set-LocationEx
    # -----
    
    param( [parameter(Mandatory = $true)][string[]]$roots )
    
    Set-StrictMode -Version Latest
    
    Write-Verbose "Set-LocationEx roots: $(Join-String -Strings $roots -Separator ', ')"
    
    function Set-LocationEx
    {
        param( [Parameter( Mandatory="true" )]$path )
    
        process
        { 
            $verbose = ( $PSCmdlet.MyInvocation.BoundParameters.ContainsKey( "Verbose" ) -and $PSCmdlet.MyInvocation.BoundParameters[ "Verbose" ].IsPresent )
            if ( $verbose )
                { Write-Verbose( "$(Join-String -Strings $roots -Separator ', ')" ) }
            if ( !( Test-Path $path ) ) 
            {
                foreach ( $p in $roots )
                { 
                    $newPath = Join-Path $p $path
                    if ( $verbose ) { Write-Verbose "Looking for $newPath" }
                    if ( Test-Path $newPath ) 
                    { 
                        $newPath = Get-PathCanonicalCase( $newPath )
                        if ( $verbose ) { Write-Verbose "Found $newPath" }
                        Push-Location $newPath
                        return
                    } 
                }
            }
            if ( Test-Path $path )
                { $path = Get-PathCanonicalCase( $path ) }
            Push-Location $path
        }
    }
    
    function Get-LocationExRoots
    {
        process
        {
            Write-Output (Join-String -Strings $roots -NewLine)
        }
    }
    
    function Get-PathCanonicalCase
    {
        param( $path )
    
        $newPath = (Resolve-Path $path).Path
        $root = [System.IO.Path]::GetPathRoot( $newPath )
        if ( $newPath -ne $root ) # Handle root directory
            { $newPath = [System.IO.Directory]::GetDirectories( $root, $newPath.Substring( $root.Length ) )[ 0 ] }
        $newPath
    }
    

提交回复
热议问题