SetEnv


SetEnv Usage Information


Introduction

I'm currently using nullsoft's excellent NSIS application to create my custom application installers, but I needed a better way of handling Environment Variables, so of course I decided to write a little utility....


Dependent upon the operating system in use, SetEnv will automatically use one of the following techniques to create/edit/delete environment variables:


Windows 95/98/ME

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 to choose when to perform the reboot.


Windows XP/2000/2K3/Vista

Under more modern (i.e. proper) operating systems, such as Windows XP, Vista, 7 and Windows 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.


Installing SetEnv

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\DebugSPY folder.


Using SetEnv

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.


User Environment Variables

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.


Creating a variable

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"


Modifying an existing variable

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.

Prefix or Postfix with expanded variables

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


Dynamic Variable Expansion

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    // Dynamic variable, when MEDIADIR changes so will MUSICDIR
    setenv -a PICDIR ~MEDIADIR~/Pics    // Second dynamic variable, PICDIR will change if MEDIADIR changes


Deleting a variable

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"


Running SetEnv from a batch file

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

Change History ([New]New, [BugFix]BugFix, [Update]Updated)


[Update] Oct 2010 (v1.9) - Updated solution to Visual Studio 2010
[BugFix] Feb 2008 (v1.8) - Fixed a problem on Windows 98 where it sometimes failed to open the Autoexec.bat file (Found by mulinux)
[Update] May 2007 (v1.7) - Added how to delete a USER environment variable to the usage information
[BugFix] Jan 2007 (v1.6) - Fixed a bug found by depaolim
[BugFix] Jan 2007 (v1.5) - Fixed a bug found by Added dynamic expansion support (same as using ~ with setx), originally requested by Andre Amaral, additional request by Synetech
[New] Sep 2006 (v1.4) - Added support to prepend (rather than append) a value to an expanded string, requested by Masuia
[New] May 2006 (v1.3) - Added support for User environment variables
[Update] Apr 2006 (v1.2) - Updated the article to explain how to use SetEnv from a batch file, after a problem was found by David Langford
[BugFix] Apr 2006 (v1.1) - Bug fix in ProcessWinXP() discovered by attiasr
[New] Oct 2005 (v1.0) - Initial Public Release.