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

利洛

我找到了一个解决方案,可以直接通过 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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在进行中,如果我们要使用该文件中定义的功能,是否不需要在同一目录中导入另一个文件?

使用CMD中的文件日期重命名文件

使用.bat(CMD)从TXT文件中删除选定的行

使用BeautifulSoup在Python中删除不需要的标签

如何通过cmd从%PATH%变量中删除目录

如何使用GetDefaultFolder获取特定文件夹并删除其创建的不需要的文件夹

使用Papaparse从CSV文件中删除不需要的列

使用python在csv中删除不需要的行

git repo长满源目录中不需要的文件

使用Python从图像中删除不需要的连接像素

如何使用python从列表中删除不需要的元素

我应该使用什么代码将脚本下载并安装到用户PATH中的用户可写目录中(不需要sudo)?

如何通过批处理文件从文本文件中删除不需要的行?

正确的方法从/ boot目录中删除不需要的文件(Ubuntu)

检查PATH中的文件是否在cmd中存在

删除脚本中不需要的文件锁定

如何使用Linux命令删除带有波浪号的不需要的文件?

使用CMD的文件关联

从CSV文件中删除不需要的列

如何从git中删除不需要的文件

如何使用Powershell或cmd删除名称中带有“ $”的文件/目录

使用cmd删除范围内的文件

使用 CMD 复制文件

使用父表删除子表中不需要的记录

Powershell Script 从 CSV 文件中删除不需要的引号

删除输出文件中不需要的字符

在 Windows 上使用 CMD 从文件目录中删除特定的子字符串

使用 bash 腳本刪除文件名中不需要的信息

在 PATH 中添加和删除目录/文件