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
精彩评论