2011-08-29 183 views

回答

6

以前不知道CDPATH。很高兴知道。我为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 

} 

它不会是完美的,但以预期的方式工作。我猜你可以扩展它。将它添加到你的个人资料。如果需要,还可以添加别名,如下所示:

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

非常好!谢谢。 –