1

I want to create a batch file to quickly switch a PATH variable in order to be able to work in a different environment, and then another to restore it. I assume that it would be done via setx and some form of search and replace, however I know naught about batch files.

1 Answer 1

3

Although this is not answering your question directly, it does solve your problem, and for that reason, I found this worth of an answer, rather than a comment.

The PATH environmental variable can store multiple paths. The order of the paths to be searched for is from left to right. This effectively means, that if you add a path to the beginning of the PATH environmental variable, it will be searched first.

This eliminates the need to replace an existing path, unless you need to ensure that the other path is removed, which this solution won't do.

To add a folder to the beginning of the PATH environmental, use the following code:

set PATH=c:\myfolder;%PATH%

The ;%PATH% is required to be there, otherwise you'll clear the path set and replace it entirely with just your own folder.

Given that the set command was used, this will only store the path for your current session. To transfer the path to the Windows Environment, so it is remembered for each session, you need to execute the following command:

setx PATH "%PATH%"

Given that you want to use it to restore it later, I would advice to create 2 batch files to launch whatever it is you want to use the different path for, and on the one that uses the alternate path, you start with the SET command, and for the one that uses the real one, you don't.

This way, for each time you run the batch file for the alternate path, this path is added and searched for first, so it finds that one.

Lastly, it is possible to set PATH based on other environmental variables, which allows you to switch.

First you need to create 2 copies of the original Path:

setx PATH_orig "%PATH%"
setx PATH_alt "%PATH%"

Now edit the PATH_alt the way you normally would edit the PATH.

Now you can simply switch by using the following commands:

:: Restore to original
setx PATH "PATH_orig"

:: Set alternate path
setx PATH "PATH_alt"
5
  • When you say session, you mean the currently open command prompt? Aug 5, 2018 at 20:54
  • Yes, or if you use this in a batch file, it will be in effect during the entire execution of the batch file from the moment you make the set, until that batchfile closes. So if that batchfile executes a program, and the batchfile remains open in the background, that path is set for that program as well.
    – LPChip
    Aug 5, 2018 at 20:56
  • I know this will come in handy but it doesn't solve my problem. I will be running external apps that will not be part of the batch file session. The idea behind the batch-file is simply to save me the trouble of having to manually edit my path every time I want to switch between my two working envs. Aug 5, 2018 at 20:59
  • There is of course also the option to store 2 different path environmental variables, modify one, and use a simple batch script to set the real path to either of these. I'll edit my answer to include that option.
    – LPChip
    Aug 5, 2018 at 21:13
  • Yep, that would do exactly what I need. Thanks! Aug 5, 2018 at 23:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .