Created
August 14, 2010 16:31
-
-
Save cl4rk3/524461 to your computer and use it in GitHub Desktop.
Batch script to manipulate arbitrary Windows environment variables from the command line
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PROJECT_ROOT=C:\development | |
JAVA_VERSION=1.5.0_11 | |
JAVA_HOME=%PROJECT_ROOT%\java%JAVA_VERSION% | |
ANT_HOME=%PROJECT_ROOT%\tools\apache-ant-1.8.1 | |
PATH=%JAVA_HOME%/bin;%PATH%;%ANT_HOME%/bin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@ECHO OFF | |
::Sets environment variables | |
:: Command Extensions must be enabled | |
:: > CMD /e:on | |
:: Special thanks to http://ss64.com/nt/ | |
SET PROPERTIES_FILE=batch.properties | |
IF [%1]==[] GOTO SHOW_HELP | |
IF /I %1==set GOTO SET_ENVIRONMENT_VARIABLES | |
IF /I %1==revert GOTO REVERT_ENVIRONMENT_VARIABLES | |
IF /I %1==clean GOTO CLEAN_ENVIRONMENT_VARIABLES | |
:SET_ENVIRONMENT_VARIABLES | |
ECHO Setting environment variables defined in %PROPERTIES_FILE% | |
FOR /F "TOKENS=1,2 DELIMS==" %%G IN (%PROPERTIES_FILE%) DO ( | |
CALL :SET_VARIABLES %%G %%H | |
) | |
GOTO END | |
:REVERT_ENVIRONMENT_VARIABLES | |
ECHO Reverting environment variables to _BACKUP versions | |
FOR /F "TOKENS=1,2 DELIMS==" %%G IN (%PROPERTIES_FILE%) DO ( | |
CALL :REVERT_PERSISTANT_VARIABLES %%G %%H | |
) | |
GOTO END | |
:CLEAN_ENVIRONMENT_VARIABLES | |
ECHO Cleaning defined environment variables | |
FOR /F "TOKENS=1,2 DELIMS==" %%G IN (%PROPERTIES_FILE%) DO ( | |
CALL :CLEAN_PERSISTANT_VARIABLES %%G %%H | |
CALL :CLEAN_CURRENT_VARIABLES %%G %%H | |
) | |
GOTO END | |
::Looped methods | |
:SET_VARIABLES | |
IF NOT "[%1]"=="[]" ( | |
IF NOT "%2"=="%1" ( | |
ECHO %1 already set. Backing up current setting to %1_BACKUP. | |
SETX %1_BACKUP %1 | |
) ELSE ( | |
ECHO %1 is already set to %2 | |
) | |
) | |
ECHO Setting %1 to %2 | |
CALL SETX %1 %2 | |
CALL SET %1=%2 | |
GOTO:EOF | |
:REVERT_PERSISTANT_VARIABLES | |
IF NOT "[%1]"=="[]" ( | |
IF NOT "%1"=="%1_BACKUP" ( | |
ECHO Reverting %1 from %2 to %1_BACKUP | |
SETX %2 %1_BACKUP | |
SET %2=%1_BACKUP | |
) ELSE ( | |
ECHO %1 is already equal to %1_BACKUP | |
) | |
) ELSE ( | |
SET %1_BACKUP=NOT_SET | |
) | |
GOTO:EOF | |
:CLEAN_PERSISTANT_VARIABLES | |
ECHO Removing %1 | |
REG delete HKCU\Environment /V %1 | |
GOTO:EOF | |
:END | |
CHOICE /C:YN /M "Would you like to close this command window" | |
IF errorlevel 2 GOTO:EOF | |
IF errorlevel 1 EXIT 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment