
This article is also published online at the following locations (With peer reviewed comments).
Simply click on the download button and then unzip and run the enclosed executable, by default the application will be installed to the C:\Program Files\Xanya\Bin folder.
The SetEnv application is a free utility for setting/updating/deleting User or System Environment Variables.
The method used to create a system wide environment is dependent upon the operating system in use, SetEnv will automatically detect this and will use one of the following techniques to create/modify or delete the environment variables.
Under Windows 9x, creating an environment variable requires modifying the user's autoexec.bat file and then executing it (or rebooting), before the variable is recognised by the Operating System.
SetEnv will automatically locate the autoexec.bat file itself (only if the file is on the C:\ or D:\ drives) and then add or modify the selected system variable. The one thing SetEnv will not do is reboot the PC as this should be left up to the user, or the Setup Kit (from Inno Setup for example) to choose when to perform the reboot.
Under more modern (i.e. proper) operating systems, such as Windows 2000, XP and Windows 2003 Server, environment variables are stored in the registry under the following key:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
Variables are added by creating a new value under this key, or by modifying a value if it already exists. To delete a variable, we simply delete its registry value, unless we are removing part of an expanded value, such as PATH in which case we only remove the part we want.
At this point Windows will not be aware of our changes unless we log off or reboot. To get around this, SetEnv will broadcast a WM_SETTINGCHANGE to all of the windows in the system. This allows other running applications, e.g. Explorer.exe to be notified of our change.
If you run SetEnv from a command prompt, then this will not update the environment variable for the current DOS window. This is mainly due to the fact that a process (SetEnv) cannot change the environment of its parent (The Command Prompt). However, any new DOS/Command Prompts that you open will show the new variable/value.
Broadcasting this message results in a slight delay (whilst the open windows process it) of around 2/3 seconds, so it may appear that SetEnv has hung, this is not the case.

SetEnv is very easy to use and has only a few command line arguments, this section will describe how to use them.
Typing SetEnv at a command prompt and then pressing the return key will make SetEnv display its usage information, which can be seen in the screen shot at the top of this page.
As of version 1.04, SetEnv also supports User environment variables, if you want SetEnv to either add or delete a User variable then add the -u option to the command line.
SetEnv can create two types of variable, a simple one that has only a single value such as:
InstallPath = C:\Program Files\MyApp\_Bin
or more complex variables with multiple values, a good example of this is the PATH variable:
PATH = C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\MyApp\_Bin
To create a simple variable just enter in the following command line, obviously substituting your own variable name and value.
SetEnv -a InstallPath "C:\Program Files\MyApp\_Bin"
Similarly, to add a variable with multiple values, you need to type the following command line for each value ensuring you prefix the value with the % character.
SetEnv -a name %value
E.g.
SetEnv -a PATH %"C:\Program Files\MyApp\_Bin"
SetEnv -a PATH %"C:\Bin"
To add a User variable instead of a System one, add the -u option as in the following example:
SetEnv -ua PATH %"C:\Program Files\MyApp\_Bin"
You modify variables in exactly the same way in which you would create them, if you are modifying a multi-value variable and you forget the % prefix to the value, then SetEnv will automatically detect the destination has multiple values and will modify it correctly.
By default SetEnv will add your new value to the end of an existing expanded variable as in this example:
PATH = %PATH%;C:\Program Files\MyApp\_Bin;NEWVALUE
I had a request from Masuia that SetEnv be modified so that it can set your new value as a Prefix to the existing variable.
The following example shows this:
PATH = NEWVALUE;%PATH%;C:\Program Files\MyApp\_Bin
To do this I have added a new command line argument to SetEnv, -p. This argument will ONLY work when modifying an existing expanded variable, and can be used with either system or user environment variables, simply add the p option to the command line.
SetEnv -ap PATH %NEWVALUE
This allows you to set a variable to be ALWAYS equal to the value of an existing environment variable, even if that variable changes.
Let me show you an example of when this would be very useful. This example was inspired by Synetech, if you mind me stealing it, just let me know.
Say that you have an environment variable called MEDIADIR (set to C:\Media) and you want to create two additional variables called MUSICDIR and PICDIR (set to %MEDIADIR%\Music and %MEDIADIR%\Pics respectively).
These would be expanded by the operating system to the following:
C:\Media\Music
C:\Media\Pics
Now, say you decided to move the MEDIADIR folder to drive D (D:\Media), using dynamic variable expansion, SetEnv will ensure that your MUSICDIR and PICDIR variables are correctly relocated to the following automatically.
D:\Media\Music
D:\Media\Pics
To enable dynamic variable expansion, simply create your variables as in the following example using the ~ symbol to identify the existing variable name.
setenv -a MEDIADIR C:\Media // Create the original variable
setenv -a MUSICDIR ~MEDIADIR~/Music // Create our dynamic variable, when MEDIADIR changes so will MUSICDIR
setenv -a PICDIR ~MEDIADIR~/Pics // Our second dynamic variable, PICDIR will change if MEDIADIR changes
To delete a variable, specify the -d option instead of the -a option as in the following example.
SetEnv -d InstallPath
To delete a value from a multi-value variable you simply enter the following, specifying the value to remove.
SetEnv -d PATH %"C:\Program Files\MyApp\_Bin"
As with modifying a multi-value variable if you forget to specify the % prefix, then SetEnv will automatically work this out and delete the specified value only.
To delete a User environment variable, simply add the -u option as follows:
SetEnv -ud PATH %"C:\Program Files\MyApp\_Bin"
SetEnv can be run sucessfully from a batch file, however a problem was found by David Langford which occurs when you attempt to specify the expanded variable prefix '%' to a value which contains a drive letter, as in the following example (This is only an issue when running SetEnv from a batch file).
SetEnv -d PATH %C:\Test
The Windows batch file interpreter will interpret the '%' character as prefix to a variable and will replace it with what it believes is the variable, %C: with the contents of that variable, which of course is nothing. This results in SetEnv being passed a modified value as shown here:
SetEnv -d PATH \Test
This is obviously wrong and SetEnv will not be able to match the value to be removed in the expanded variable, it will thus fail to delete the value from the variable.
The workaround is to escape the '%' character with another '%' character. The following example will correctly remove the C:\Test value from the expanded variable, PATH.
SetEnv -d PATH %%C:\Test