Determinadas Variáveis não se expandirão nas Variáveis de Ambiente do Sistema Windows PATH [duplicado]

3

Por alguma razão, há certas variáveis de ambiente que não serão expandidas quando usadas na variável "PATH" nas Variáveis do sistema.

Por exemplo, %SystemRoot% funciona bem, enquanto %WinDir% não funciona. %ProgramFiles% e %ProgramFiles(x86)% também não funcionam.

Obviamente, sei que posso usar o caminho completo em vez de uma variável, mas esse não é o ponto.

Alguma idéia?

Aqui está uma captura de tela / exemplo:

    
por XenonKilla 01.02.2016 / 04:23

1 resposta

4

SystemRoot expande como esperado porque é uma variável de ambiente pseudo / predefinida. WinDir é uma variável de ambiente regular e "compete" com outros, como PATH na sequência de inicialização.

Melhor explicado por Raymond Chen em Segredos do Windows: as variáveis ocultas : " Incorporar uma variável de ambiente dentro de outra é simplesmente uma questão de bom tempo operacional ". Citando mais:

Here’s how the environment-building process works. It proceeds in roughly four steps:

  • First, the system creates some predefined machine-wide environment variables, such as System­Root and ALL­USERS­PROFILE (but not COMPUTER­NAME or Program­Files).
  • Second, it creates environment variables from the System section of the Environment Variables dialog box. The System environment variable definitions can use the “%” notation to refer to the predefined environment variables created in the previous step. For example, you can set a System environment variable to %System­Drive%\Extras. After the System environment is complete, Windows starts building the User environment.
  • Step three is to create predefined per-user environment variables, such as USER­PROFILE and APP­DATA. The COMPUTER­NAME and Program­Files-related variables are also created here, even though they’re technically System variables and not per-user variables.
  • Finally, the system creates the environment variables. These are in the User section of the Environment Variables dialog box and have access to any variables created by the first three steps, so you can set a User environment variable to %USER­PROFILE%\Extras or a custom System environment variable set in the second step. If a User environment variable has the same name as a System environment variable, the new value replaces the old.

...

One customer was having difficulty setting the System PATH environment variable to %APPDATA%;C:\Windows. They found the final environment merely contained the literal path as specified (percent signs and all), instead of replacing it with the value of the APPDATA environment variable. If you look through the sequence of operations previously detailed, it’s clear why this occurred. They were trying to set a System environment variable based on a variable that had not yet been defined.

The solution was simple: Move editing the PATH from the System environment list box to the User environment list box. That way, when it wanted to use the %APPDATA% environment variable, the variable would be there.

Para um exemplo simples de possíveis "condições de corrida" ao definir variáveis de ambiente baseadas em outras, considere o caso circular onde se definem duas variáveis de sistema como:

bbb=%ccc%
ccc=%bbb%

No meu Windows 7, isso resulta nas variáveis que avaliam:

C:\etc>set
...
bbb=%ccc%
ccc=%ccc%
    
por 01.02.2016 / 05:36