Question

I was looking for a way to set the environment path variable with a .cmd file.
When the path variable was getting too long, I got some errors.
Just add the needed variables to 'Set Path variable' below
Check the current value of your path variable and add to the script
Run the script as administrator!
Open a new console window and it should work e.g. php -v

Was it helpful?

Solution

@ECHO OFF

:: %HOMEDRIVE% = C:
:: %HOMEPATH% = \Users\Ruben
:: %system32% ??
:: No spaces in paths
:: Program Files > ProgramFiles
:: cls = clear screen
:: CMD reads the system environment variables when it starts. To re-read those variables you need to restart CMD
:: Use console 2 http://sourceforge.net/projects/console/


:: Assign all Path variables
SET PHP="%HOMEDRIVE%\wamp\bin\php\php5.4.16"
SET SYSTEM32=";%HOMEDRIVE%\Windows\System32"
SET ANT=";%HOMEDRIVE%%HOMEPATH%\Downloads\apache-ant-1.9.0-bin\apache-ant-1.9.0\bin"
SET GRADLE=";%HOMEDRIVE%\tools\gradle-1.6\bin;"
SET ADT=";%HOMEDRIVE%\tools\adt-bundle-windows-x86-20130219\eclipse\jre\bin"
SET ADTTOOLS=";%HOMEDRIVE%\tools\adt-bundle-windows-x86-20130219\sdk\tools"
SET ADTP=";%HOMEDRIVE%\tools\adt-bundle-windows-x86-20130219\sdk\platform-tools"
SET YII=";%HOMEDRIVE%\wamp\www\yii\framework"
SET NODEJS=";%HOMEDRIVE%\ProgramFiles\nodejs"
SET CURL=";%HOMEDRIVE%\tools\curl_734_0_ssl"
SET COMPOSER=";%HOMEDRIVE%\ProgramData\ComposerSetup\bin"
SET GIT=";%HOMEDRIVE%\Program Files\Git\cmd"

:: Set Path variable
setx PATH "%PHP%%SYSTEM32%%NODEJS%%COMPOSER%%YII%%GIT%" /m

:: Set Java variable
setx JAVA_HOME "%HOMEDRIVE%\ProgramFiles\Java\jdk1.7.0_21" /m

PAUSE

OTHER TIPS

I come at this question with a Linux perspective. Usually setting an environment variable in Linux ($myVar=1) only sets it for the current process but not any child process that it spawns.

To allow any child process to read a variable you need to export envVar=2. In Windows the set command already does this for you. This is generally what you want.

The setx command sets a variable permanently for the current user, but strangely enough this is not reflected in the current process, you will need to open another cmd.exe window for it to take effect.

C:\> set foobar=1

C:\> powershell "echo ${env:foobar}"
1

C:\> setx barfoo 2

SUCCESS: Specified value was saved.

C:\> powershell "echo ${env:barfoo}"  # not present

C:\> 

Also notice the strictly necessary syntax difference between set and setx.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top