本文介绍了在PowerShell中CDPATH功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人实施在PowerShell中的等价行为?

Has anyone implemented the equivalent behavior of bash's 'cdpath' in Powershell?

推荐答案

之前并不知道CDPATH。很高兴知道。我刮起了下面的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

这篇关于在PowerShell中CDPATH功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 15:12