Lidar com a variável Path é pegajosa, pois é uma combinação das variáveis Path do sistema e Path do usuário. As respostas anteriores não explicam isso. Por exemplo
SETX PATH %PATH%;C:\MyFolder
definirá o caminho do usuário para todo o caminho atual do sistema, mais o caminho do usuário e, em seguida, anexará '; C: \ MyFolder' a ele. Se eu tivesse usado SETX /M PATH %PATH%;C:\MyFolder
, o caminho do sistema receberia o caminho do usuário atual adicionado a ele.
Usar SETX
ou SETX /M
é bom para qualquer variável de ambiente, exceto o Caminho. Infelizmente, lidar com as variáveis Path é uma tarefa difícil, pois envolve a atualização do registro e a possibilidade de anexar um novo valor. Primeiro, devemos copiar a entrada do registro em uma variável de ambiente, acrescentar o diretório que estamos adicionando ao caminho e gravar retorna ao registro.
Existe outro problema, que é o de que as variáveis Path são armazenadas como strings REG_EXPAND_SZ e é comum que um caminho do sistema contenha referências a %SystemRoot%
. Isso significa que qualquer mecanismo que você use para ler, manipular e, em seguida, escrever a variável Path, idealmente não deve expandir nada.
Por fim, é comum não haver uma variável Path do usuário, o que significa que o código que atualiza o Path precisa considerar o erro que você obterá se tentar ler uma variável que não existe.
Veja alguns exemplos de código que podem atualizar o caminho atualizando os valores no registro.
@echo off
setlocal ENABLEDELAYEDEXPANSION
rem
rem Add c:\bin to the path
rem
rem
rem
rem There are two values we can use for the PATHKEY. The one that starts
rem with HKEY_LOCAL_MACHINE sets the path that's used by all processes and
rem users on the system. The PATHKEY that starts with HKEY_CURRENT_USER
rem updates the part of the Path that only visible to processes running
rem under the current user account.
rem
set PATHKEY=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
set PATHKEY=HKEY_CURRENT_USER\Environment
set PATHVAR=Path
set TMPFILE=%TEMP%\addpath.txt
set ADDPATH=c:\bin
rem
rem Read the Path value from the registry into a file. I could have coded
rem this with no temporary file by using:
rem for /f "delims=" %%i in ('REG QUERY "%PATHKEY%" /v "%PATHVAR%" 2>nul:') do set XLINE=%%i
rem However, having the temporary file was useful for debugging and as
rem updating the path is something we don't often do we don't care if
rem doing so is a bit slower.
rem
REG QUERY "%PATHKEY%" /v "%PATHVAR%" >"%TMPFILE%" 2>nul:
if errorlevel 1 goto :newpath
rem
rem REG QUERY outputs several lines. We only care about the last non-blank line
rem Fortunately, a 'for' loop ignores blank lines.
rem
for /f "delims=" %%i in (%TMPFILE%) do set XLINE=%%i
rem
rem Extract the value from the Path variable. Here's what we expect to see
rem in XLINE though with with spaces shown as ~ symbols:
rem
rem ~~~~Path~~~~REG_EXPAND_SZ~~~~Path-is-here..."
rem
rem See below for other ways we can extract the path value from XLINE.
rem
for /f "tokens=1,2,* delims= " %%i in ("!XLINE!") do set VARNAME=%%i & set VARTYPE=%%j & set XVALUE=%%k
rem
rem Append an element to the Path.
rem
set NEWPATH=!XVALUE!;!ADDPATH!
rem
rem Update the path
rem
REG ADD "%PATHKEY%" /v "%PATHVAR%" /t REG_EXPAND_SZ /d "!NEWPATH!" /f
goto :done
rem
rem The Path variable does not exist and so create it
rem
:newpath
REG ADD "%PATHKEY%" /v "%PATHVAR%" /t REG_EXPAND_SZ /d "!ADDPATH!"
goto :done
rem
rem Delete the temporary file.
rem
:done
del "%TMPFILE%"
endlocal
goto :eof
rem
rem Here are some variations for parsing XLINE to extract the value.
rem
rem
rem Quick and dirty method. It takes advantage of that REG QUERY returns a
rem line with four spaces, the variable name which we know is four
rem characters, four spaces, the variable type which we know is
rem REG_EXPAND_SZ and is 13 characters, four spaces, and then the value. As
rem some systems may be using REG_SZ Path strings the quick and dirty method
rem seems like a bad idea.
rem
set XVALUE=!XLINE:~29!
rem
rem One flaw with the method I used in the code above is that you are
rem allowed to have spaces in variable names. Here's a slight modification
rem to the code to support spaces in variable names. It takes advantage of
rem the fact that REG VIEW always puts four spaces each of the fields. We
rem first translate instances of four spaces into vertical bars and use that
rem character as the delimiter when parsing the line.
rem
rem I could have used any character as the delimiter as long as it's one
rem that will not appear in the variable name, the variable type, or as the
rem first character(s) of the variable's value. Some people use a tab here.
rem I chose not to do that in this example as the tabs are not visible.
rem
rem The code still has a flaw in that it will fail if the variable name
rem contains four consecutive spaces.
rem
set XLINE=!XLINE: =^|!
for /f "tokens=1,2,* delims=|" %%i in ("!XLINE!") do set VARNAME=%%i & set VARTYPE=%%j & set XVALUE=%%k