Modify Path

Last Modified: Sat, 04/21/2012 - 04:22

Introduction

Modify Path is a fairly simple Windows script that allows users to easily modify the path environment variable by adding a new or removing an existing directory.  It was written to facilitate installation of Inno Setup packages that require path changes.  As such, it is likely to be more useful for developers and administrators than end-users.

Note:  I've rewritten Modify Path in native Inno Setup Pascal script.  Since this was my primary use for Modify Path, I'll likely only update the Pascal script going forward.  However, feel free to e-mail me if you have a need for an updated AutoIt script as well.  I'll be happy to assist if I'm able.

Return to top

Download  Current Version: 1.4.2, Released: 04/21/2012

ModPath Inno Setup Source (6.79 KB) - This is the Inno Setup Pascal script source.  An example installer package script is also available.

Legacy ModPath AutoIt Binary (119 KB) - This is the compiled AutoIt ModPath executable

Legacy ModPath AutoIt Source (3.16 KB) - The AutoIt source code for ModPath

ChangeLog - ModPath development details

Requirements

Inno Setup
version 5.4.0 or greater
License
GNU Lesser General Public License (LGPL), version 3

Return to top

Installation and Usage (Inno Setup)

To use ModPath in your Inno Setup package, follow these steps:

  1. Copy modpath.iss to the same directory as your setup script
  2. Add this statement to your [Setup] section
    • ChangesEnvironment=true
  3. Add this statement to your [Tasks] section;  You can change the Description or Flags.  You can also change the Name (as of version 1.4), but it must match the ModPathName setting below.
    • Name: modifypath; Description: Add application directory to your environmental path; Flags: unchecked
  4. Add the following to the end of your [Code] section;  ModPathName defines the name of the task defined above.  ModPathType defines whether the user or system path will be modified; this must be either system or usersetArrayLength must specify the total number of dirs to be added;  Result[0] contains first directory, Result[1] contains second (optional), etc.
    • const
          ModPathName = 'modifypath';
          ModPathType = 'user';

      function ModPathDir(): TArrayOfString;
      begin
          setArrayLength(Result, 1)
          Result[0] := ExpandConstant('{app}');
      end;
      #include "modpath.iss"

Once the above statements have been included and compiled into your package, the environmental path will be automatically updated with the main application directory upon installation and uninstallation if the modifypath task is enabled.

Return to top

Legacy Installation and Usage (AutoIt)

ModPath has no installation process, per se.  If you want to use the AutoIt script on your system, simply download it to any folder in your default system path.  C:\WINNT\ or C:\WINDOWS\ (whichever is appropriate) is usually a good choice.

Usage of the program should be very straightforward.  The syntax for ModPath is modpath.exe {/add | /del} <path>.  This states that modpath.exe should be called, told whether to add a new directory to or remove an existing directory from the current path, then given the location of the new directory.  Be sure to enclose the location in quotes if it contains a space.

For example, the following command will add the "New Application" directory to the system path:
modpath.exe /add "C:\Program Files\New Application"

Conversely, the next command will remove the same directory if it already exists in the path:
modpath.exe /del "C:\Program Files\New Application"

Return to top

Technical Details

Note: The following description was written for the AutoIt version of ModPath, but it also applies to the Inno Setup version.

ModPath will add or remove the specified directory to the path under both Windows 9x and NT (in the context, NT refers to any NT-based version of Windows, including 2000 and XP).  However, removal support is somewhat limited under Windows 9x.  ModPath can be used to remove any directory currently in the environmental path from Windows NT, but it can only remove directories that it had previously added itself from Windows 9x.  This is because Windows 9x provides multiple methods of setting the path, which makes it difficult to account for every possibility.

Under Windows NT, the system path is stored as a semi-colon delimited list in the registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\[Path], and the user path is stored in the same format under HKEY_CURRENT_USER\Environment\[Path].  When run, ModPath tokenizes the appropriate path into an array, and loops through each entry.  If adding a directory and it already exists, it simply exists; otherwise, it appends it to the end of the current path.  If removing a directory, it deletes any existing matching entries.  It then writes the new key back to the registy.  The updated path will take effect immediately in all newly-spawned shells.

Under Windows 9x, ModPath uses C:\autoexec.bat to store the path.  If adding a directory, it searches through autoexec.bat to first check if it already exists.  If adding a directory and already exists, it simply; exists; otherwise, it adds a new line appending the directory to the existing path.  If removing a directory, it deletes any previously added lines to autoexec.bat that match the specified directory.

Path changes take effect immediately under Windows NT, but require a reboot under Windows 9x.  A workaround for Windows 9x is to use the winset.exe utility (found on any Windows 98 CD-ROM).  This utility can be used to set the path for all future processes, though it doesn't modify the currently running explorer.exe, nor does it permanently set the path.  It makes a good compliment to ModPath when used during an installation.

Return to top