1 (изменено: Rumata, 2014-02-08 02:12:57)

Тема: CMD/BAT: удаление путей из переменной PATH

Полагаю, что все понимают важность и необходимость перменной PATH. Понадбилось реализовать как можно блее простой способ удаления заданных путей из переменной PATH. Сделал  делюсь, встречайте скрипт UNPATH, который
-- удаляет заданные пути из PATH
-- удаляет пути соответствующие шаблону
-- очищает PATH полностью
-- отображает содержимое

Удаление производится только для текущего окружения и не сохоаняется в системе для всех процессов.

Примеры


:: показать текущее содержимое
UNPATH

:: удалить только c:\windows
UNPATH c:\windows

:: удалить все, что начинается на c:\windows
UNPATH /s c:\windows

:: удалить все, что начинается на c:\Program Files
:: удалить только c:\windows
UNPATH /s "C:\Program Files" /l c:\windows

:: очистить PATH (аналогично PATH ;)
UNPATH PATH

Исходный код


::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

Оригинал на google.code

( 2 * b ) || ! ( 2 * b )