views:

521

answers:

2

Not being a windows VBS writer (but user of Windows shells) I use multiple different versions of third-party code which require me to set windows shell environment variables, specifically these are Java related

Looking at the following question I began to ponder what I would need to do to quickly change shell environment variables with a single command. Eg:

In mode A i have

ANT_HOME=c:\foo\bar\ant-1.5.1
JAVA_HOME=c:\foo\java\1.4
PATH=c:\foo\java\1.4\bin;c:\foo\bar\ant-1.5.1\bin

In mode B to develop other things, I desire a quick switch to the following (also culling the PATH settings for the above in the process)

ANT_HOME=c:\foo\bar\ant-1.6.1
JAVA_HOME=c:\foo\java\1.6
PATH=c:\foo\java\1.6\bin;c:\foo\bar\ant-1.6.1\bin

To augment the script in the referenced question I'm guessing some path processing using the Split command and so forth will be necessary. Has anyone already done something like this that they would be willing to share?

I obviously can't just kill the whole PATH since there are numerous other items in the PATH which I want to preserve which are used.

+1  A: 

Use the ANT_HOME and JAVA_HOME variables in your path:

PATH=%JAVA_HOME%\bin;%ANT_HOME%\bin

Have a batch files in your paths for swapping between the 2 contexts

modea.bat:

set ANT_HOME=c:\foo\bar\ant-1.5.1
set JAVA_HOME=c:\foo\java\1.4

modeb.bat:

set ANT_HOME=c:\foo\bar\ant-1.6.1
set JAVA_HOME=c:\foo\java\1.6
Tom
Should mention I already do something along these lines. I was thinking more - one script to rule them all kind of thing. Not something I would ordinarily bother writing, since I have a solution, but perhaps someone has spent the time before me doing just that.
j pimmel
+1  A: 

Building on thaggie's answer, you can pass the ANT_HOME and JAVA_HOME paths as arguments to your script:

C:> MyScript c:\foo\java\1.4 c:\foo\bar\ant-1.5.1

This way you wouldn't need to manage a .bat script for each "mode".

Then in your script use the arguments in the path:

:: Not needed, but makes it clearer which argument is the Ant path 
:: and which is the Java path.
set JAVA_HOME=%1
set ANT_HOME=%2

:: If you want to preserve the current path and just append the Ant and
:: Java paths:
set PATH=%path%;%JAVA_HOME%;%ANT_HOME%

:: If you want the path to just be the Ant and Java paths:
set PATH=%JAVA_HOME%;%ANT_HOME%
Patrick Cuff