Reputation: 9240
I am trying to set the PATH environment variable in windows 7 using a bat-file; however it does not seem to work.
I am using this windows command:
set PATH=%cd%;%path%
pause
However it only appears to be valid for this cmd instance. I want it to be permanent, since I first set the PATH and then run a program which needs to locate the libraries in that folder.
Upvotes: 47
Views: 85013
Reputation: 798
Use This command setx PATH "%PATH%;%MVN_HOME%\bin\"
Anyways it wont be set in current session you need to use
set PATH="%PATH%;%MVN_HOME%\bin\"
Upvotes: 0
Reputation: 1
Assuming I want to create a System Environment Variable called "ZIP_PROGRAM" and I want to point it to the executable at path "reg add C:\Program Files\7-Zip\7z.exe
I will perform following at DOS Prompt:
Step1: execute following code reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v ZIP_PROGRAM /t REG_SZ /d "C:\Program Files\7-Zip\7z.exe" /f
Step2: Log Off then Login
Step3: Open DOS Prompt and execute: "set z" and you should be able to see the System Environment Variable update
Upvotes: 0
Reputation: 2483
A simple (may be better) solution is to use PathMgr.cmd
Down the pathmgr_1.0.2.zip in https://gallery.technet.microsoft.com/Batch-Script-To-Manage-7d0ef21e
Unzip and put the pathmgr.cmd in the same folder as your batch file, then in your batch file write these two lines:
call pathmgr.cmd /del %cd% /y
call pathmgr.cmd /add %cd% /y
This will:
1) only update the user variable PATH, 2) will not include system PATH multiple times
You can also run the batch file multiple times, and it will only include your current path ONCE in the PATH.
Upvotes: 4
Reputation: 1980
If you want to do it in a batch file, use the reg command to change the path value in the registry at the HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment key.
Something like:
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path /t REG_SZ /d "%path%;c:\newpath"
Check that the path in the %path% variable matches the system path.
Upvotes: 21
Reputation: 8254
As wizlb noted, doing
setx PATH "%cd%;%path%;" -m
will copy local env to system env, and without -m it will copy system env to user env. Neither is desirable. In order to accurately edit only one part of registry (system or user, system in my below example) you need to do this:
for /F "tokens=2* delims= " %%f IN ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path ^| findstr /i path') do set OLD_SYSTEM_PATH=%%g
setx.exe PATH "%OLD_SYSTEM_PATH%;%OTHER_STUFF%;" -m
Credit for the solution goes to http://www.robvanderwoude.com/ntregistry.php
Upvotes: 18
Reputation: 9266
Use setx.exe instead of set.
setx PATH "%cd%;%path%;"
pause
Note that this sets the path for all future cmd instances, but not for the current one. If you need that, also run your original set command.
UPDATE: The second parameter needs to be quoted if it contains spaces (which %path% always has). Be warned that if the last character in your %path% is a backslash, it will escape the trailing quote and the last path entry will stop working. I get around that by appending a semicolon before the closing quote.
If you don't want to risk getting ";;;;;;" at the end of your path after repeated runs, then instead strip any trailing backslash from the %path% variable before setting, and it will work correctly.
Upvotes: 66
Reputation: 612854
To do this properly I think you really need to go beyond a simple batch file. The MSDN documentation states:
To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a
WM_SETTINGCHANGE
message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates.
First of all you won't be able to write to that key without a UAC elevation prompt. That's best arranged by adding the appropriate manifest to an executable file. Secondly, broadcasting WM_SETTINGCHANGE
isn't simple from a batch file.
In your position I'd write a short and simple console app to do the job.
Upvotes: 12