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.
3 Answers
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.
8 Comments
setlocal to your batch file, the path is only visible in the fileSET is really something you should know and understand before you do any scripting.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. :-/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
2 Comments
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.set PATH=%PATH%;C:\newpath; and set PATH="%PATH%;C:\newpath;" didn't work, but set "PATH=%PATH%;C:\newpath;" did.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.
5 Comments
/m needs to be after setx?/m at the end, the DESIRED_PATH was added with /m at the end for current user only.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.