windows – 如何编写.bat或.cmd文件以从PATH中删除元素?

有关:
How to list the elements of the path in a batch file?
How does FOR work?

如何编写批处理文件或CMD文件以从路径中删除元素?它应该优雅地处理:

>案件的差异
>短名和长名

我使用tr.exe完成了这个,但是它很慢而且复杂并且使用了临时文件,这使得它变得更加复杂.

我认为答案是这样的:

setlocal
set tpath="" 
set _path="%PATH:;=" "%"
for %%p in (%_path%) do (
   call :KeepIfNotEqual %%p %elementToRemove% 
)
endlocal & set path=%tpath%

…其中%elementToRemove%是要删除的路径元素. KeepIfUnique必须是一个带有两个参数的子例程 – 目录名,规范化它们,如果它不等于第二个参数(elementToRemove),则将第一个参数附加到tpath.

正如我所说,我可以使用tr.exe执行此操作,但是我可以使用windows cmd.exe shell中的内置命令来执行此操作吗?

编辑:我想当你明白它,问题是,如何在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,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%

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...