开发者

Is there any way to set a recursive environment variable in Windows?

开发者 https://www.devze.com 2023-02-13 16:05 出处:网络
I have an a开发者_如何学运维pps directory in my dropbox - I\'d like to be able to access all of them from the command line without having to set up loads and loads of path variables. Is there any way

I have an a开发者_如何学运维pps directory in my dropbox - I'd like to be able to access all of them from the command line without having to set up loads and loads of path variables. Is there any way to set up a recursive path variable? I tried putting ** at the end - no joy.


You can't use placeholders or anything like that in the PATH environment variable. It's just a concatenation of directories, no additional features.

So either add all of the app directories to the PATHenvironment variable or think about other ways to solve the problem. For example, you could add one directory to the PATH and place batch files named like the apps there that start the apps.


Made an account for this 11 year old question.

    $path = Read-Host -Prompt "Enter the exact path that needs to be recursively added to the PATH env:"
    $items = gci -Path $path -Recurse -Directory -name
    $nuPath = $env:Path
    $r = 0
    write-Host "Env started as $nuPath"
    foreach ($iitem in $items){
    $addpath = ($path + "\" + $iitem)
    $executabledir = $addpath + '\' + "*.exe"
    if(test-path $executabledir){
    Write-Host $addpath
    $regexAddPath = [regex]::Escape($addPath)
    $arrPath = $nuPath -split ';' | Where-Object {$_ -notMatch "^$regexAddPath\\?"}
    $nuPath = ($arrPath + $addPath) -join ';'
    ++$r
    }
    }
    $result = ($path + ";" + $nupath) -join ';'
    $temp = New-TemporaryFile
    $result.ToString() > $temp
    Start-Process notepad.exe -ArgumentList $temp.FullName
    
    $title    = 'WARNING'
    $question = "Your new environmental variable for PATH will be in the notepad window that popped up. are you sure you want to continue?"
    $choices  = '&Yes', '&No'
    
    $decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
    if ($decision -eq 0 -and $r -gt 5) {
        $title    = 'Are you really sure?'
        $question = 'This is larger than 5 entries and this can ruin your day if you mess it up. Just doublechecking everything is OK'
        $choices  = '&Yes', '&No'
    
        $decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
            if ($decision -eq 0) {
            $env:Path > $HOME\pathbkup.txt
            [Environment]::SetEnvironmentVariable("Path", $result, "Machine")
            }
            else {
            Write-Host 'cancelled'
            }
    }
    else {
        Write-Host 'cancelled'
         }
    Remove-Item $temp
0

精彩评论

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