UNPATH: the script for removing elements from the PATH

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

UNPATH: the script for removing elements from the PATH

#1 Post by siberia-man » 07 Feb 2014 13:40

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
Last edited by siberia-man on 07 Feb 2014 16:13, edited 2 times in total.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

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

#2 Post by Squashman » 07 Feb 2014 15:24

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.

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

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

#3 Post by siberia-man » 07 Feb 2014 15:27

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? =)

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

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

#4 Post by Squashman » 07 Feb 2014 15:40

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.

siberia-man
Posts: 208
Joined: 26 Dec 2013 09:28
Contact:

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

#5 Post by siberia-man » 07 Feb 2014 16:02

Squashman,

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

Post Reply