I am writing an uninstallation script, so I would like to 'undo' modifications the installation made to the system. In achieving this goal, I would like to parse the PATH
variable, and remove any values that the installation added to the PATH
.
To do so, I developed the following pseudocode -
- Save the contents of
PATH
to a temporary variable - Split the
PATH
into tokens, using the;
character as a delimiter, and loop through each token - (In Loop) Identify if the current token is one added by the installation
- (In Loop) If the current token was not added by the installation, save it to be added to the updated
PATH
(in a temporary variable) - Save the updated
PATH
I expected this to be relatively straightforward to implement.
The first step, storing the PATH
is simple.
SET TEMP_PATH=%PATH%
However, when I try to loop through each token, it will not work as I expected.
FOR /F "delims=;" %%A IN (%TEMP_PATH%) DO ECHO %%A
This command only outputs the first token, and no subsequent tokens are echoed out.
So, I have two questions -
- How can I loop through an unknown number of tokens and work with each one?
- Is there another way to achieve the same goal which may be simpler?
Thank you.
This command only outputs the first token, and no subsequent tokens are echoed.
Use the following batch file.
SplitPath.cmd:
Example Output:
Notes:
for
loop as appropriate to implement the rest of your pseudocode.Further Reading
The batch code below removes 1 or more folder paths as defined at top of the script with
PathToRemove1
,PathToRemove2
, ... fromHKEY_CURRENT_USER\Environment
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
The update of system PATH requires administrator privileges which means the batch file must be executed as administrator if user account control (UAC) is not disabled for the user account executing the batch file.
The batch code works only for Windows Vista and later versions of Windows because of command SETX is by default not available on Windows XP or even former versions of Windows.
For availability of command SETX see SS64 article about SetX and Microsoft's SetX documentation.
For the
reg.exe
output differences on Windows XP versus later Windows versions see Rob van der Woude's Reading NT's Registry with REG Query. The different output ofreg.exe
is taken into account by the batch code below.For an explanation why not using local
PATH
as currently defined on execution of the batch file read the questions, answers and comments ofCommented batch code for folder path removal from user and system PATH:
The batch code above uses a simple case-insensitive string substitution and a case-sensitive string comparison to check if the current path to remove is present in user or system PATH. This works only if it is well known how the folder paths were added before and the user has not modified them in the meantime. For a safer method of checking if PATH contains a folder path see the answer on How to check if directory exists in %PATH%? written by dbenham.
Attention: This batch code is not designed to handle the very rare use cases of system or user
PATH
contain a folder path with one or more semicolons in path string enclosed in double quotes to get the;
interpreted by Windows inside the double quoted folder path string as literal character instead of separator between the folder paths.For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
for /?
goto /?
if /?
reg /?
reg add /?
reg delete /?
reg query /?
rem /?
set /?
setlocal /?
setx /?
See also Microsoft's article about Using command redirection operators for an explanation of
>nul
and2>nul
with redirection operator>
being escaped with^
to use the redirection on execution ofreg.exe
instead of interpreting2>nul
misplaced for command FOR which would result in an exit of batch processing by Windows command interpreter because of a syntax error.