I am trying to add the current directory (from a command-line) to Windows path permanently, but I am having serious problems implementing this.
My initial attempt was:
set PATH=%PATH%;%cd%
However, this only works in the current session; as soon as I close the command-line window, the PATH
environment variable retains its previous value.
Next, I tried:
setx PATH=%PATH%;%cd%
This might work in Windows 7 and 8 according to some of the answers that I found here, but in Windows 10, the setx
command has three ways of working:
Syntax 1:
SETX [/S system [/U [domain\]user [/P [password]]]] var value [/M]
Syntax 2:
SETX [/S system [/U [domain\]user [/P [password]]]] var /K regpath [/M]
Syntax 3:
SETX [/S system [/U [domain\]user [/P [password]]]]
/F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]
Long story short, I am unable to get it to work:
ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Can somebody please suggest how to complete my goal the easiest way?
If there's a different syntax per Windows version, then I'd be happy to get this info as well.
Thank you very much!!!
Simple solution:
The syntax of
setx
is slightly different than the syntax ofset
.With
set
, I would do:With
setx
, I needed to do:The first argument specifies the name of the environment variable.
The second argument specifies the value to add to this variable.
The double-quotes are required for when the second argument contains spaces.
This might also be the case with
set
, by the way.As described in detail in answer on Why are other folder paths also added to system PATH with SetX and not only the specified folder path? it is not good to modify system or user
PATH
from within a batch file by simply overwriting or appending a folder path toPATH
stored in registry using the localPATH
.One solution for this task to add current directory path to user
PATH
is using this code on Windows Vista or later versions of Windows:The disadvantage of this solution is a user
PATH
being finally for exampleC:\Temp;C:\Temp\Other Folder;C:\Temp\One More Folder
when current directory is firstC:\Temp
, on next run of the batch fileC:\Temp\Other Folder
andC:\Temp\One More Folder
on third execution of the batch file.The solution to avoid this is defining an application specific environment variable called in next batch file
MyAppPath
which is always overwritten on execution of the batch file. To the userPATH
is added only the reference of the environment variableMyAppPath
.In this case user
PATH
as stored in registry contains always just%MyAppPath%
and registry value is of type REG_EXPAND_SZ, but the value of environment variableMyAppPath
also stored in registry, but of type REG_SZ, is updated to current directory path on each execution of the batch file. So the userPATH
in registry does not get longer and longer on each execution of a batch file from a different folder than before.In general an application or application suite is not good coded if its application folder or one of its subfolder must be in local
PATH
on execution of the application or any application from the suite to work properly at all. The application or application suite can store its installation path also somewhere else in registry likeApp Paths
or in a file in a subfolder of%APPDATA%
(user account related standard application data path) from which it can be read on next run. An installer package should modify user or systemPATH
only if this application is most likely executed mainly from within a command prompt window by a user.For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
reg /?
reg query /?
set /?
setlocal /?
setx /?
Following should be also read:
>nul
.App Paths
.PATH
.