Page 1 of 1

UNPATH: the script for removing elements from the PATH

Posted: 07 Feb 2014 13:40
by siberia-man
Once I decided to to implement the simple tool to cleanup the PATH variable removing some elements there. I'd like to do this in the easiest way that would allowed to remove a set of elements at once. I did it. Here you are. There is the UNPATH tool having opposite effect to the PATH command.

I tried to implement it very similar to the PATH command as much as possible. But some of existing bug in the dos-shell doesn't allow to complete them. In any way, the script (written in batch completely) does the requested features:
-- remove certain paths
-- remove paths matching the specified pattern (if the directory is starting with the defined pattern)
-- clean up the PATH
-- show the existing PATH

Removing is performed for the current environment only and is not kept in the system permanently for all processes.

Code: Select all

::Displays or unsets a search path for executable files.
::
::UNPATH [[/S|/L] path] ...
::UNPATH PATH
::
::Type UNPATH PATH to clear all search-path settings snd direct cmd.exe to
::search only in the current directory. It is similar to PATH ;.
::
::Type UNPATH without parameters to display the current path.
::
::Type a set of directories to be removed from the PATH. Use switches to
::modify behavior of the command:
::    /S - Tells to remove all entries starting with the path
::    /L - Tells to remove the exact match of the path (by default)
@echo off

if "%~1" == "/?" (
   for /f "tokens=* delims=:" %%a in ( 'findstr /b "::" "%~f0"' ) do echo:%%a
   goto :EOF
)

if "%~1" == "" (
   path
   goto :EOF
)

if /i "%~1" == "path" (
   path ;
   goto :EOF
)

setlocal

set "unpath_subdir="
for %%a in ( %* ) do call :unpath_arg "%%~a"

if defined PATH set "PATH=%PATH:~1%"

endlocal & set "PATH=%PATH%"
goto :EOF


:unpath_arg
if "%~1" == "" goto :EOF

if /i "%~1" == "/S" (
   set "unpath_subdir=1"
   goto :EOF
)

if /i "%~1" == "/L" (
   set "unpath_subdir="
   goto :EOF
)

for /f "tokens=*" %%s in ( "%PATH:;=" "%" ) do (
   set "PATH="
   for %%p in ( "%%s" ) do call :unpath_entry "%%~p" "%~1"
)
goto :EOF


:unpath_entry
if "%~1" == "" goto :EOF
if /i "%~1" == "%~2" goto :EOF

set "unpath_entry=%~1"
call set "unpath_entry=%%unpath_entry:%~2=%%"

if /i "%~1" == "%~2%unpath_entry%" if "%unpath_entry:~0,1%." == "\." if defined unpath_subdir goto :EOF

set "PATH=%PATH%;%~1"
goto :EOF



If the community found the script useful I would updated the topic with the actual version of the script. In any way all of you can find the latest version following by the link http://code.google.com/p/cmd-bat/source ... unpath.bat

Re: UNPATH: the script for removing elements from the PATH

Posted: 07 Feb 2014 15:24
by Squashman
But this would only change the path for the current environment you are in. As soon as you close that environment out and start a new one the PATH would change back to whatever is set in the registry.

To permanently change the path variable you would either need to edit the registry directly or use the SETX command.

Re: UNPATH: the script for removing elements from the PATH

Posted: 07 Feb 2014 15:27
by siberia-man
Squashman wrote:But this would only change the path for the current environment you are in. As soon as you close that environment out and start a new one the PATH would change back to whatever is set in the registry.

To permanently change the path variable you would either need to edit the registry directly or use the SETX command.


Exactly! It works almost similar to PATH or SET PATH=, in other words it performs changes to the current dos session. Have you expected something else? =)

Re: UNPATH: the script for removing elements from the PATH

Posted: 07 Feb 2014 15:40
by Squashman
siberia-man wrote:
Squashman wrote:But this would only change the path for the current environment you are in. As soon as you close that environment out and start a new one the PATH would change back to whatever is set in the registry.

To permanently change the path variable you would either need to edit the registry directly or use the SETX command.


Exactly! It works almost similar to PATH or SET PATH=, in other words it performs changes to the current dos session. Have you expected something else? =)

I just wasn't sure if you knew that it would not change the path permanently. Some new person who comes along and reads this thread is probably going to think it will permanently change the path variable but it will not. You should specify specifically what your script does.

Your thread title should probably say
UNPATH: the script for temporarily removing elements from the PATH for the current environment.

Re: UNPATH: the script for removing elements from the PATH

Posted: 07 Feb 2014 16:02
by siberia-man
Squashman,

Agree. I understood your point. I added one sentence in bold to the first port.