开发者

CDPATH functionality in Powershell?

Has anyo开发者_如何学Pythonne implemented the equivalent behavior of bash's 'cdpath' in Powershell?


Did not know about CDPATH before. Good to know. I whipped up the below for Powershell:

function cd2 {
    param($path)
    if(-not $path){return;}

    if((test-path $path) -or (-not $env:CDPATH)){
        Set-Location $path
        return
    }
    $cdpath = $env:CDPATH.split(";") | % { $ExecutionContext.InvokeCommand.ExpandString($_) }
    $npath = ""
    foreach($p in $cdpath){
        $tpath = join-path $p $path
        if(test-path $tpath){$npath = $tpath; break;}
    }
    if($npath){
        #write-host -fore yellow "Using CDPATH"
        Set-Location $npath
        return
    }

    set-location $path

}

It will not be perfect, but works in the expected way. You can extend it I guess. Add it to your profile. If needed, also add an alias like so:

set-alias -Name cd -value cd2 -Option AllScope
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜