你如何编写一个.bat或.cmd文件以从PATH中删除一个元素? [英] How would you write a .bat or .cmd file to remove an element from the PATH?

查看:237
本文介绍了你如何编写一个.bat或.cmd文件以从PATH中删除一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关:

如何列出批处理文件中路径的元素?

FOR如何工作?

如何编写批处理文件或CMD文件以从路径中删除元素?应妥善处理:

How would you write a batch file or CMD file to remove an element from the path? It should handle gracefully:



  • 短名称和长名称之间的差异

我使用tr.exe做了这个,但是它慢,复杂,使用临时文件,这使它更复杂。

I've done this using tr.exe but it's slow and complicated and uses temporary files, which makes it even more complicated.

我认为答案如下:

I think the answer is something like this:

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

...其中%elementToRemove%是要删除的路径元素。 KeepIfUnique将必须是一个子程序,它接受两个参数 - 目录名称,规范化它们,并将第一个参数追加到tpath如果它不等于第二个参数(elementToRemove)。

...where %elementToRemove% is the path element to remove. KeepIfUnique would have to be a subroutine that takes two arguments - directory names, normalizes them, and appends the first argument to tpath if it is not equal to the 2nd argument (elementToRemove).

正如我所说,我可以用tr.exe,但是我可以用Windows cmd.exe shell中的内置命令来做到这一点?

As I said, I can do this with tr.exe, but can I do it with just built-in commands in the windows cmd.exe shell?

编辑:我想当你一直到它,问题是,如何做case转换cmd.exe? / p>

EDIT: I guess when you get right down to it, the question is, how to do case-conversion in cmd.exe?

推荐答案

这是我想出的,使用Igor的提示。

This is what I came up with, using Igor's hint.

@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%

这篇关于你如何编写一个.bat或.cmd文件以从PATH中删除一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆