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.

Berming
  • 1,302
  • 4
  • 18
  • 34

1 Answers1

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

RealHowTo
  • 34,977
  • 11
  • 70
  • 85
drhanlau
  • 2,517
  • 2
  • 24
  • 42
  • 1
    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... – Julian Mann Jun 21 '12 at 16:46
  • 5
    @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 and then running the command only once. Be sure to execute the command with admin rights if you use the `/M` flag. – Marcus Pope Jul 16 '12 at 16:27
  • @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. – Julian Mann Jul 16 '12 at 17:11
  • 4
    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... – SSIsyphe Dec 07 '17 at 16:02
  • 1
    __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. – Mofi May 31 '19 at 11:44
  • For more information on how to modify persistent stored __user__ or __system__ `PATH` see for example [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/a/25919222/3074564) and [Adding the current directory to Windows path permanently](https://stackoverflow.com/a/47080452/3074564) and [How to search and replace a string in environment variable PATH?](https://stackoverflow.com/a/24650324/3074564) and everything referenced by these answers. – Mofi May 31 '19 at 11:55