17

I'm trying to write a script that when clicked will add a couple of entries to the PATH Environment variable in Windows, instead of making the changes manually. I see .bat files being used all the time on Windows for a variety of reasons, so can a .bat script help me with something like that?

I actually need to download a zip from a location on the Internet, extract it to a specified location, then update the PATH in environment variable. Never done this before so any hints appreciated.

1 Answer 1

38

If you wish to change/update the PATH permanently in the environment variable, you can use the SETX command e.g.

setx path "%PATH%;C:\New Folder" 

For more details information on %PATH% and other variables to access to system folder, refer to http://vlaurie.com/computers2/Articles/environment.htm

Sign up to request clarification or add additional context in comments.

6 Comments

I tried this, and the value of %PATH% (the system variable) is prepended to "C:\New Folder". So far so good. However, it is saved as the user path variable. The path variable is then made up from the system path prepended to the user path. The result is that all the system path directories are there twice and "C:\New Folder" is stuck on the end. And the next time you try to append something, you get the system directories 3 times and so on...
@JulianMann You are probably looking for the /M switch which will set the system environment instead of the user environment. Running it multiple times will definitely duplicate the information because %PATH% is a variable referencing the current environment that you just modified - it appends the data and is not intended to be modified multiple times. I recommend editing the path var manually to remove duplicates <computerhope.com/issues/ch000549.htm> and then running the command only once. Be sure to execute the command with admin rights if you use the /M flag.
@Marcus thanks for the info. In my case I was giving the bat script to users (who don't have admin rights) so they could set their paths to point at my software. Its something that is fairly easy to do in bash on a unix based system. If the variable is not there, set it, otherwise append to it. The fact that the PATH is made from 2 parts on windows throws a spanner in the works.
WARNING ! SETX is deadly dangerous, as it crops the PATH to 1024 characters (yes, we are in 2017, Windows 10). To me, using it made me lose a part of my PATH and 2 hours of my time. Not a relevant solution, except if you're looking for trouble...
NO, NO and once more NO. This line corrupts the user PATH environment variable. It uses local PATH with all folder paths of system and user PATH with all environment variables expanded to set user PATH with C:\New Folder appended. This line is really, really bad and should be never used by anyone reading this answer. It is awful. The usage of local PATH to set user or system PATH is an absolute NO GO, NEVER EVER.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.