我找到了一个解决方案,可以直接通过 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
现在总是返回零。就我而言,这已经奏效了......:/此外,使用当前代码(添加%
到所有函数参数),无论pause
调用:removeFromPath
. 真气人!
如果要使用另一个变量作为变量替换的一部分,则需要使用延迟扩展。您还需要处理您要删除的路径位于结尾的可能性,%path%
因此不会以;
.
@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%
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句