加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 01:51:21 所属栏目:Windows 来源:网络整理
导读:有关: How to list the elements of the path in a batch file? How does FOR work? 如何编写批处理文件或CMD文件以从路径中删除元素?它应该优雅地处理: 案件的差异 短名和长名 我使用tr.exe完成了这个,但是它很慢而且复杂并且使用了临时文件,这使得它变
有关:
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%

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读