我发现solution通过Windows命令行直接从环境变量PATH中删除某个目录。但我想通过从cmd文件中发出命令来达到相同的结果。使用cmd文件从PATH中删除不需要的目录
什么我能做的就是呼应我想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
但是与解决途径仍保持不变。代码完美无缺地工作,如果我没有将删除步骤的范围缩小,因为我想为多个目录执行该操作,如果没有这样一个好帮手为我完成这项工作真的很痛苦。
EDIT 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
现在总是返回零。就我而言,这已经奏效了......:/此外,使用当前代码(在所有函数参数中添加了%
),在调用:removeFromPath
后,cmd总是直接关闭,而不管pause
。真是令人生气!
为什么你想从'%PATH%'变量删除位置?您是否知道,您在批处理/ cmd.exe会话中使用'SET'PATH =%PATH:match = replace%“进行的任何更改都不是先决条件,它只会持续与cmd实例一样长。你能否提供更多的批处理文件,以便我们更好地处理你希望达到的目标。 – Compo
@Lilo,脚本中是否有其他功能可能导致问题?我测试了安德斯的脚本,确实确实删除了路径中的目录。 –
从长远来看,我想使用setx,但只要它不能与set一起工作,为什么我会使用setx? 我已经安装了ActivePerl以及es StrawberryPerl,我希望能够通过简单地双击批处理脚本来切换当前使用的Perl变体。为了实现这一点,我必须检查哪些目录当前在PATH中,因此必须删除或添加以从当前使用的变体切换到另一个。 – Lilo