225

Basically, I know I can go through my control panel and modify the path variable. But, I'm wondering if there is a way to through batch programming have a temporary path included? That way it is only used during that batch file execution. I don't want to have people go in and modify their path variables just to use my batch file.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
michael
  • 14,844
  • 28
  • 89
  • 177

3 Answers3

369

Just like any other environment variable, with SET:

SET PATH=%PATH%;c:\whatever\else

If you want to have a little safety check built in first, check to see if the new path exists first:

IF EXIST c:\whatever\else SET PATH=%PATH%;c:\whatever\else

If you want that to be local to that batch file, use setlocal:

setlocal
set PATH=...
set OTHERTHING=...

@REM Rest of your script

Read the docs carefully for setlocal/endlocal , and have a look at the other references on that site - Functions is pretty interesting too and the syntax is tricky.

The Syntax page should get you started with the basics.

fejese
  • 4,601
  • 4
  • 29
  • 36
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 5
    If you add a `setlocal` to your batch file, the path is only visible in the file – jeb Jul 26 '11 at 16:50
  • hw do you add "setlocal"? Is that in place of "SET"? – michael Jul 26 '11 at 21:04
  • @micheal: updated my answer with links. Please read some documentation/examples/tutorials about cmd scripting - `SET` is really something you should know and understand before you do any scripting. – Mat Jul 26 '11 at 21:13
  • 2
    Life saver. I needed to add an EV on a server that couldn't be restarted and this allows me to do what I need without having to schedule a restart! – dav_i Aug 30 '13 at 14:15
  • 10
    Even without `SETLOCAL` it's, at most, only going to be for that individual command prompt session -- if you're doing this from more than one batch file, and using `EXIT /B %N%`, then `SETLOCAL` is basically just going to dump your changes when the script exits. :-/ – BrainSlugs83 Apr 03 '14 at 18:29
  • Why do you use `@REM` and not just `REM`? What does the `@` do? – HelloGoodbye Mar 31 '16 at 11:37
  • @HelloGoodbye: `@` suppresses echo for that line – Mat Mar 31 '16 at 12:00
  • Is there a way to make a change to the system path which is immediate (No need to Restart) yet holds just until the next restart? Thank You. – Royi Aug 14 '16 at 20:15
34

There is an important detail:

set PATH="C:\linutils;C:\wingit\bin;%PATH%"

does not work, while

set PATH=C:\linutils;C:\wingit\bin;%PATH%

works. The difference is the quotes!

UPD also see the comment by venimus

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • 12
    in fact it should be `SET "PATH=...%PATH%"` else spaces existing in path will cause errors or misbehavior. Wrapping in quotes like this will not include them but will properly set the variable. Same works for any other env variable. – venimus Sep 02 '19 at 11:04
  • Thank you @venimus for your comment. Both `set PATH=%PATH%;C:\newpath;` and `set PATH="%PATH%;C:\newpath;"` didn't work, but `set "PATH=%PATH%;C:\newpath;"` did. – Burhan Dec 22 '21 at 18:58
23

That's right, but it doesn't change it permanently, but just for current command prompt.

If you wanna to change it permanently you have to use for example this:

setx ENV_VAR_NAME "DESIRED_PATH" /m

This will change it permanently and yes, you can overwrite it in another batch script.

bnieland
  • 6,047
  • 4
  • 40
  • 66
dumbak
  • 404
  • 3
  • 8
  • Maybe `/m` needs to be after `setx`? – suzanshakya Mar 03 '14 at 09:01
  • Works the same for me, but this Win Batch Scripting is little unfinished business from MS. In my company where we have all the same laptops with same system there is not a universal script for PATH setting and we are maintaining 2 or 3 of them, so whatever works for you :) – dumbak Mar 04 '14 at 09:40
  • Thanks for the update. In Windows Server 2012, when I put `/m` at the end, the `DESIRED_PATH` was added with /m at the end for current user only. – suzanshakya Mar 05 '14 at 05:52
  • 12
    A word of caution. Using setx on a path, trying to add more to it, and the string is > 1024 long, I discovered that the result (my environment PATH) was truncated permanently. (A good thing that I had could copy the %PATH% I had ECHOed out before the change, and put it back using the Environment variables windows tool.) – Andreas Jansson May 29 '15 at 10:23
  • 2
    Attention! This command directly overwrite the System Environment Variable! I had deleted them and try to fix them. And also for anyone who did this mistake: do not restart your computer. Write `echo %path%` and you'll get the current loaded path. You need to distinguish the User and System environment variables from each other. Then you can manually apply them to the correct place. – Alper Dec 04 '19 at 13:08