Skip navigation

JSI Tip 8840. EditPath.exe freeware is a Win32 console program that allows you to manipulate the current user and system path environment variables in the registry.

Download epath2.zip and extract the files to a folder that is in your PATH.

The EditPath.txt file contains:

EditPath - (C) 2004 by Bill Stewart ([email protected])

LICENSE

This program may be used freely. You may redistribute it provided you keep all
the files in this archive together.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.

DESCRIPTION

EditPath is a Win32 console program that allows you to manipulate the current
user and system path environment variables in the registry.

SYSTEM REQUIREMENTS

Windows NT 4.0 or later (this includes Windows 2000, XP, 2003, etc.).

BACKGROUND

Windows uses the PATH environment variable to locate programs. For example,
here's a simplified overview of what happens when you type a command at the
command prompt:

1.  If the command name includes a directory name, the command interpreter
    (usually cmd.exe) checks if the program file exists in the specified
    directory. If it's there, then the command interpreter runs the program;
    otherwise, it reports an error.

2.  If the command name does not include a directory name, the command
    interpreter checks the current directory for the program file. If the
    program file exists in the current directory, the command interpreter runs
    it; otherwise, it skips to the next step.

3.  The command interpreter searches for the program file in each directory
    specified in the PATH environment variable, in order. If it exists in any
    of the directories specified in the PATH variable, the command interpreter
    runs it; otherwise, it reports an error.

Directories in the PATH variable are separated by semicolons (;). Each
individual directory is referred to as a "path element."

The operating system maintains two values in the registry that contain
persistent (e.g. maintained across logons) PATH information. The first registry
subkey is:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

The Path value in this subkey applies to the entire system, no matter who is
logged on, and is referred to as the "system path."

The second registry subkey is:

HKEY_CURRENT_USER\Environment

The Path value in this subkey applies only to the current user and is referred
to as the "current user path."

Both Path value entries should be of registry data type REG_EXPAND_SZ, which
means that they can contain environment variable names to be expanded enclosed
by percent signs (%).

Path elements and environment variable names are not case-sensitive.

USING EDITPATH

EditPath provides the ability to individually add or remove path elements from
either the current user path or the system path (provided you have permission).
It can also list the current user and system path elements, and check if a
particular path element exists in the current user or system path.

The command-line syntax is as follows:

EditPath \[-s\] \{\[-q\] \{-a | -r | -c\} directory | -l \[-x\]\}

-s specifies the system path. If you don't use -s, EditPath uses the current
user path.

-q (only with -a, -r, or -c) tells EditPath to run quietly.

-a or -r adds or removes a directory name from the path.

-c checks if a directory name exists in the path. If the directory name is
present in the path, EditPath will return an exit code of 0. If it is not
present, it will return a non-zero exit code.

-l lists each path element, one per line. -x performs environment variable
expansion on the path elements.

The -a, -r, -c, and -l options are mutually exclusive. -q can only be used with
-a, -r, or -c, and -x can only be used with -l. -s can be used with any
options. Options can appear in any order on the command line.

If the directory name contains spaces, enclose it in quotes.

When specifying a path element on the command line, you can specify the name of
an environment variable by surrounding it with tilde (~) symbols. EditPath will
replace each ~ with a percent sign (%).

If EditPath successfully updates the registry with -a or -r, it broadcasts a
message to the system that the PATH has been updated. This will not affect the
command window from which EditPath is run.

It's important to understand how EditPath evaluates path elements. For example,
suppose the current user path contains the following:

C:\BIN\Tools;C:\BIN\Scripts

The above line contains two path elements: C:\BIN\Tools and C:\BIN\Scripts.
EditPath won't let you add "C:\BIN\Tools" or "C:\BIN\Scripts" because they
already exist. Also, you can't remove "C:\BIN" because it's not one of the
elements, even though the literal string "C:\BIN" appears twice.

When adding a path element, it is always appended to the end of the existing
path.

EXAMPLES

1.  Add the directory C:\SCRIPTS to the current user path:

    editpath -a c:\scripts

2.  Add %SystemDrive%\Scripts to the system path:

    editpath -s -a ~systemdrive~\scripts

3.  Script fragment to check if %SystemDrive%\Scripts exists in the system
    path:

    editpath -q -s -c ~SystemDrive~\Scripts
    if %ERRORLEVEL% EQU 0 (echo Present) else (echo Not present)

4.  List the system path:

    editpath -s -l

    Sample output:

    %SystemRoot%\system32
    %SystemRoot%
    %SystemRoot%\system32\Wbem

5.  List the system path, expanding the environment variables:

    editpath -s -l -x

    Sample output:

    C:\WINDOWS\system32
    C:\WINDOWS
    C:\WINDOWS\system32\Wbem

6.  Remove C:\NTRESKIT from the system path:

    editpath -s -r c:\ntreskit

7.  Add %ProgramFiles%\My Program to the current user path:

    editpath -a "~ProgramFiles~\My Program"

PROGRAM MESSAGES

Note that you can suppress these messages if you specify the -q option on the
command line (except where noted).

Error: This program requires Windows NT 4.0 or later.

    This message appears if you attempt to run EditPath on Windows 9x/Me. The
    exit code is 120 (ERROR_CALL_NOT_IMPLEMENTED). The -q option does not
    suppress this message.

The command completed successfully.

    EditPath ran successfully and no errors were returned. The exit code is 0
    (NO_ERROR).

Error X reading registry value.

    EditPath attempted to read the Path value from the registry, but failed.
    The error number is the error returned by the Win32 API. The exit code is
    the error number. On Windows 2000 and later, you can see the textual
    description of the error code by using the command NET HELPMSG x (where "x"
    is the error code).

Error: Registry data type not valid.

    EditPath successfully read the Path value from the registry, but it was not
    of type REG_EXPAND_SZ or REG_SZ. The exit code is 13 (ERROR_INVALID_DATA).
    This message should be fairly rare.

Registry value not found.

    The Path registry value does not exist. The exit code is 2
    (ERROR_FILE_NOT_FOUND).

Found the specified path element.

    The command line contains -c, and the path element exists. The exit code is
    0 (NO_ERROR).

Path element not found.

    The command line contains -c, but the path element does not exist. The exit
    code is 2 (ERROR_FILE_NOT_FOUND).

Error: Access denied.

    The current user does not have permission to perform the action. The exit
    code is 5 (ERROR_ACCESS_DENIED).

Error: Unable to add or remove path element.

    EditPath was unable to internally add or remove a path element. This error
    should be quite rare. The exit code is 13 (ERROR_INVALID_DATA).

Error: Specified path element already exists.

    You attempted to add a path element when it already exists. The exit code
    is 183 (ERROR_ALREADY_EXISTS).

KNOWN ISSUES

If the Path environment value in the registry contains two consecutive
semicolon delimiters, EditPath will not work properly because it considers two
consecutive delimiters to be the end of the string. If I get a lot of trouble
reports about this, I will write a workaround, but the current behavior should
be sufficient.

TECHNICAL INFORMATION

EditPath was written for and compiled with Free Pascal 1.0.10.

VERSION HISTORY

1.0 (30 Nov 2004)

*   Initial version.



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish