Related:
如何列出批处理文件中路径的元素? https://stackoverflow.com/questions/1424558
FOR 是如何运作的? https://stackoverflow.com/questions/817280/how-does-for-work-in-cmd-batch-file/1293667#1293667
如何编写批处理文件或 CMD 文件来从路径中删除元素?它应该优雅地处理:
我已经使用 tr.exe 完成了此操作,但它速度慢且复杂,并且使用临时文件,这使得它更加复杂。
I think答案是这样的:
setlocal
set tpath=""
set _path="%PATH:;=" "%"
for %%p in (%_path%) do (
call :KeepIfNotEqual %%p %elementToRemove%
)
endlocal & set path=%tpath%
...其中 %elementToRemove% 是要删除的路径元素。 KeepIfUnique 必须是一个子例程,它接受两个参数 - 目录名称,对它们进行规范化,并将第一个参数附加到 tpath(如果是)不等于到第二个参数(elementToRemove)。
正如我所说,我可以使用 tr.exe 来完成此操作,但是我可以仅使用 Windows cmd.exe shell 中的内置命令来完成此操作吗?
EDIT:我想当你认真思考时,问题是,如何在 cmd.exe 中进行大小写转换?
这是我根据伊戈尔的提示想到的。
@echo off
goto START
-------------------------------------------------------
rmpath.bat
remove a path element from path
Created Tue Sep 15 21:33:54 2009
-------------------------------------------------------
:START
SETLOCAL ENABLEDELAYEDEXPANSION
@REM require one argument (the path element to remove)
if _%1==_ goto USAGE
@REM ~fs = remove quotes, full path, short names
set fqElement=%~fs1
@REM convert path to a list of quote-delimited strings, separated by spaces
set fpath="%PATH:;=" "%"
@REM iterate through those path elements
for %%p in (%fpath%) do (
@REM ~fs = remove quotes, full path, short names
set p2=%%~fsp
@REM is this element NOT the one we want to remove?
if /i NOT "!p2!"=="%fqElement%" (
if _!tpath!==_ (set tpath=%%~p) else (set tpath=!tpath!;%%~p)
)
)
set path=!tpath!
@call :LISTPATH
goto ALL_DONE
-------------------------------------------------------
--------------------------------------------
:LISTPATH
echo.
set _path="%PATH:;=" "%"
for %%p in (%_path%) do if not "%%~p"=="" echo %%~p
echo.
goto :EOF
--------------------------------------------
--------------------------------------------
:USAGE
echo usage: rmpath ^<arg^>
echo removes a path element from the path.
goto ALL_DONE
--------------------------------------------
:ALL_DONE
ENDLOCAL & set path=%tpath%
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)