使用cmd文件从PATH中删除不需要的目录

时间:2017-09-01 11:39:59

标签: windows batch-file cmd environment-variables

我发现solution直接通过Windows命令行从环境变量PATH中删除某个目录。但是我希望通过在cmd文件中发出命令来实现相同的结果。

我可以做的是回显我想要设置PATH的结果,如果我把

echo %PATH:D:\dir-path;=%

进入我的cmd文件。但如果我写

set PATH=%PATH:D:\dir-path;=%

PATH仅包含我想删除的目录。

此外,我想在导演路径中使用变量,但如果我尝试,结果会更糟。我把

set dirPath=D:\dir-path
echo %PATH:%dirPath%;=%

我得到的只是dirPath;=

我不知道如何解决这个问题,所以如果有人可以提供帮助我真的很感激。

编辑1

根据@Anders的建议,我现在有:

@echo off

set exampleRemoveDir=d:\bar

REM If I use %exampleRemoveDir% it does not work at all.
call :removeFromPath exampleRemoveDir & set result=%ERRORLEVEL%

:removeFromPath
set removeDir=%~1

setlocal enableextensions enabledelayedexpansion
set _=!PATH:%removeDir%;=!
if "%_%" == "%PATH%" set _=!PATH:%removeDir%=!
endlocal & set PATH=%_%
echo %PATH%
exit /b 0

但是使用该解决方案,PATH仍然保持不变。如果我没有将删除步骤的范围缩小,那么代码可以完美地运行,因为我想为多个目录执行此操作,如果没有这样一个好帮手为我做这项工作将会非常痛苦。

编辑2

因为这似乎比我想象的要复杂得多,所以到目前为止我完全没有改变脚本。

@echo off
::  Switching Perl version from ActivePerl to StrawberryPerl or the other way round depending on which paths are set in
::  PATH.

::  Directories for ActivePerl
set activePerl_SiteBinPath=D:\ProgramFiles\ActivePerl\site\bin
set activePerl_BinPath=D:\ProgramFiles\ActivePerl\bin

::  Directories for StrawberryPerl
set strawberryPerl_SiteBinPath=D:\ProgramFiles\StrawberryPerl\perl\site\bin
set strawberryPerl_BinCPath=D:\ProgramFiles\StrawberryPerl\c\bin
set strawberryPerl_BinPath=D:\ProgramFiles\StrawberryPerl\perl\bin

:: Determine which of the directories are present in PATH and which are not.
call :isInPath %activePerl_SiteBinPath% & set foundActivePerl_SiteBinPath=%ERRORLEVEL%
call :isInPath %activePerl_BinPath% & set foundActivePerl_BinPath=%ERRORLEVEL%
call :isInPath %strawberryPerl_SiteBinPath% & set foundStrawberryPerl_SiteBinPath=%ERRORLEVEL%
call :isInPath %strawberryPerl_BinCPath% & set foundStrawberryPerl_BinCPath=%ERRORLEVEL%
call :isInPath %strawberryPerl_BinPath% & set foundStrawberryPerl_BinPath=%ERRORLEVEL%

:: Test
call :removeFromPath %strawberryPerl_SiteBinPath% & set removedStrawberryPerl_SiteBinPath=%ERRORLEVEL%

rem if /i %foundActivePerl_SiteBinPath% equ 0 if /i %foundActivePerl_BinPath% equ 0 (
rem     if /i %foundStrawberryPerl_SiteBinPath% neq 0 if /i %foundStrawberryPerl_BinPath% neq 0 if  /i %foundStrawberryPerl_BinCPath% neq 0 (
rem         echo Switching from ActivePerl to StrawberryPerl.
rem         TODO
rem         exit /b 0
rem     )
rem )
rem 
rem if /i %foundStrawberryPerl_SiteBinPath% equ 0 if /i %foundStrawberryPerl_BinPath% equ 0 if /i %foundStrawberryPerl_BinCPath% equ 0 (
rem     if /i %foundActivePerl_SiteBinPath% neq 0 if /i %foundActivePerl_BinPath% neq 0 (
rem         echo Switching from StrawberryPerl to ActivePerl.
rem         TODO
rem         exit /b 0
rem     )
rem )

:: Error
:: TODO
exit /b

:isInPath
::  Tests if the path stored within variable pathVar exists within %PATH%.
::
::  The result is returned as the ERRORLEVEL:
::      0 if the pathVar path is found in %PATH%.
::      1 if pathVar path is not found in %PATH%.
::      2 if parhVar path is missing or undefined.

:: Error checking
if "%~1"=="" exit /b 2

set pathVar=%~1

for /f %%i in ('echo ";%%PATH%%;" ^| find /c /i ";%pathVar%;"') do set /a foundPathVar=%%i

if /i %foundPathVar% equ 0 (
    exit /b 1
)
exit b/ 0

:removeFromPath
::  Removes a given directory from environment variable PATH if the directory exists within PATH.
::
::  The result is returned as the ERRORLEVEL:
::      0 if the given directory was removed from PATH or if it didn't exist in PATH.
::      1 if no directory was given or the directory is undefined.

:: Error checking
if "%~1"=="" exit /b 2

set removeDir=%~1

setlocal enableextensions enabledelayedexpansion
set _=!PATH:%removeDir%;=!
if "%_%" == "%PATH%" set _=!PATH:;%removeDir%=!
endlocal & set PATH=%_%
echo %PATH%

exit /b 0

编辑3

感谢@Anders从路径中删除目录现在工作正常。我删除了上面未定义参数的错误检查,并在函数调用的参数周围添加了%。 但不知怎的,:isInPath现在总是返回零。就我而言,这已经有效......:/此外,使用当前代码(将%添加到所有函数参数),cmd总是直接关闭,而不管pause调用{ {1}}。这是令人愤怒的!

1 个答案:

答案 0 :(得分:2)

如果要将另一个变量用作变量替换的一部分,则需要使用延迟扩展。您还需要处理您要删除的路径位于;末尾的可能性,因此不会被@echo off set removedir=d:\bar set PATH=c:\foo;%removedir%;y:\baz&REM Set example %path% echo Starting with %%PATH%% set to %PATH% setlocal enableextensions enabledelayedexpansion set _=!PATH:%removedir%;=! if "%_%" == "%PATH%" set _=!PATH:;%removedir%=! endlocal & set PATH=%_% echo %%PATH%% is now %PATH% 终止。

@echo off
goto start

:removeFromPath 
echo.Debug: Starting function with remove=%~1 and %%PATH%%=%PATH%
setlocal enableextensions enabledelayedexpansion
set removedir=%~1
set _=!PATH:%removedir%;=!
if "%_%" == "%PATH%" set _=!PATH:;%removedir%=!
endlocal & set PATH=%_%
echo.Debug: Ending function with %%PATH%%=%PATH%
goto :EOF


:start
set exampleremovedir=d:\bar
set PATH=c:\foo;%exampleremovedir%;y:\baz&REM Set example %path%

echo Starting with %%PATH%% set to %PATH%

call :removeFromPath %exampleremovedir%

echo %%PATH%% is now %PATH%

这里作为辅助函数:

pip install selenium chromedriver_installer