Windows 使用cmd文件从路径中删除不需要的目录
我找到了一个直接通过Windows命令行从环境变量PATH中删除某个目录的方法。但是我希望通过在cmd文件中发出命令来实现相同的结果 如果我把Windows 使用cmd文件从路径中删除不需要的目录,windows,batch-file,cmd,environment-variables,Windows,Batch File,Cmd,Environment Variables,我找到了一个直接通过Windows命令行从环境变量PATH中删除某个目录的方法。但是我希望通过在cmd文件中发出命令来实现相同的结果 如果我把 echo %PATH:D:\dir-path;=% 输入我的cmd文件。但是如果我写 set PATH=%PATH:D:\dir-path;=% 路径仅包含我要删除的目录 此外,我想为控制器路径使用一个变量,但如果我尝试,结果会更糟。我把 set dirPath=D:\dir-path echo %PATH:%dirPath%;=% 而我得到的只是
echo %PATH:D:\dir-path;=%
输入我的cmd文件。但是如果我写
set PATH=%PATH:D:\dir-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
但有了这一解决方案,道路仍然没有改变。如果我不确定删除步骤的范围,那么代码就可以完美地工作,因为我想对多个目录执行删除步骤,如果没有这么好的助手为我完成这项工作,那将是一件非常痛苦的事情
编辑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,从path中删除目录现在可以正常工作了。我删除了上面未定义参数的错误检查,并在函数调用的参数周围添加了%
。
但不知何故,:isInPath
现在总是返回零。就我而言,这已经奏效了…:/此外,对于当前代码(将%
添加到所有功能参数中),无论调用:removeFromPath
后出现暂停
,cmd始终直接关闭。真是气人 如果要使用另一个变量作为变量替换的一部分,则需要使用延迟扩展。您还需要处理这样一种可能性,即您要删除的路径位于%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%
为什么要从%PATH%
变量中删除位置?您是否知道,在批处理/cmd.exe会话中使用SET“PATH=%PATH:match=replace%”
所做的任何更改都是无效的,它只会持续到该cmd实例。您能否提供更多批处理文件,以便我们更好地处理您希望实现的目标。@Lilo,您的脚本中是否有其他可能导致问题的函数?我测试了Anders的脚本,它确实从路径中删除了dir。从长远来看,我想使用setx,但只要它不能与set一起工作,我为什么要使用setx?我已经安装了ActivePerl和es StrawberryPerl,我希望能够通过双击批处理脚本来切换当前使用的Perl变体。为了实现这一点,我必须检查哪些目录当前在路径中,因此必须删除或添加以从当前使用的变体切换到另一个。如果将%exampleRemoveDir%更改为exampleRemoveDir%,这将永远不会起作用。在编辑中,在调用:removeFromPath之后不会停止脚本,批处理的常规部分将作为脚本的一部分执行:removeFromPath!复制并粘贴我的整个示例,然后再试一次…@Anders好吧,我已经发布了我到目前为止的所有信息。如果我使用%exampleRemoveDir%
甚至不会发出:removeFromPath
中的回音。但我想我在另一个层面上做错了什么…--只要我不将其限定在函数中,这就非常有效。如果我将函数:removeFromPath
放在我设置removedir=%~1
的位置,然后使用您的代码,即使该函数中的回音显示未更改的路径。我这里遗漏了什么?@Lilo您是否尝试过在函数内部设置本地enabledelayedexpansion
。@GerhardBarnard是的,我尝试过。我看不出有多大区别,但它不会起作用…:(@Anders我注意到,如果exampleremovedir
在路径的末尾,它会在路径的末尾留下一个;
path@GerhardBarnard:谢谢,现在应该修好了