windows 通过批处理从 %path% 变量中删除不需要的路径名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21289762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:18:38  来源:igfitidea点击:

Remove unwanted path name from %path% variable via batch

windowsbatch-filepath

提问by user3208239

Scope: Windows XP or newer Tools: Batch script

适用范围:Windows XP 或更新版本工具:批处理脚本

I need to be able to remove an unneeded path name from the system %PATH% variable. I know how to add a new path name to the system %PATH% variable, using a tool such as SETX.EXE, which also makes it immediately available within the existing CMD environment. It's probably a matter of using FIND and/or a FOR loop of some kind, but I'm not quite sure how to accomplish this. Here's a sample path statement...

我需要能够从系统 %PATH% 变量中删除不需要的路径名。我知道如何使用诸如 SETX.EXE 之类的工具向系统 %PATH% 变量添加新路径名,这也使其在现有 CMD 环境中立即可用。这可能是使用 FIND 和/或某种 FOR 循环的问题,但我不太确定如何实现这一点。这是一个示例路径语句...

%PATH% = C:\;C:\Program Files\Common Files\Java;C:\oracle\product.2.0\bin;C:\WINDOWS;C:\WINDOWS\system32;

From this, I need to be able to remove the full path name related to "oracle." So, in the above example, I need to be able to remove the "C:\oracle\product\10.2.0\bin" from the above path statement. Unfortunately, not only could the oracle path name be different than shown above, there could be multiple oracle path names and all need to be removed. I tried implementing the solution here...

由此,我需要能够删除与“oracle”相关的完整路径名。所以,在上面的例子中,我需要能够从上面的路径语句中删除“C:\oracle\product\10.2.0\bin”。不幸的是,不仅 oracle 路径名可能与上面显示的不同,而且可能有多个 oracle 路径名并且都需要删除。我尝试在这里实施解决方案......

How can I extract a full path from the PATH environment variable?

如何从 PATH 环境变量中提取完整路径?

However, it just isn't working. The script wouldn't find the path name. Any help would be appreciated. Thank you.

但是,它不起作用。脚本找不到路径名。任何帮助,将不胜感激。谢谢你。

采纳答案by SachaDee

You can try something like this :

你可以尝试这样的事情:

@echo off&cls
setlocal EnableDelayedExpansion
set $line=%path%
set $line=%$line: =#%
set $line=%$line:;= %

for %%a in (%$line%) do echo %%a | find /i "oracle" || set $newpath=!$newpath!;%%a
set $newpath=!$newpath:#= !
echo set path=!$newpath:~1!

I putted an echoto the last line. Check the result and If it's OK for you, remove it.

我把 anecho放在最后一行。检查结果,如果对您没问题,请将其删除。

回答by Jens A. Koch

This removes the substring C:\Program Files (x86)\Git\bin;from the PATH string and re-assigns:

C:\Program Files (x86)\Git\bin;将从 PATH 字符串中删除子字符串并重新分配:

set PATH=%PATH:C:\Program Files (x86)\Git\bin;=%

You might use this to see the change:

您可以使用它来查看更改:

echo %PATH:C:\Program Files (x86)\Git\bin;=% | tr ; \n

Note: be exact on the substring. It's case-sensitive and slash-sensitive.

注意:在子字符串上要准确。它区分大小写和斜线敏感。

If you need to make it a persistent change use setxinstead of setand open another console for changes to take effect.

如果您需要使其成为持久更改,请使用setx而不是set打开另一个控制台以使更改生效。

setx /M PATH "%PATH:C:\Program Files (x86)\Git\bin;=%"

回答by Joe

After trying SachaDee's answers I got errors with paths like

在尝试 SachaDee 的答案后,我遇到了类似路径的错误

C:\Program Files (x86)

with brackets: Program Files (x86)\Directorygave me

带括号: Program Files (x86)\Directory给了我

Directorywas unexpected at this time.(no matter what time I tried it)

目录was unexpected at this time.(无论我什么时候尝试过)

I added

我加了

set $line=%$line:)=^^)%

before the for-loop and

在 for 循环之前和

set $newpath=!$newpath:^^=!

after the loop (not sure if it is necessary)

循环后(不确定是否有必要)

@echo off
setlocal EnableDelayedExpansion
set path
set $line=%path%
set $line=%$line: =#%
set $line=%$line:;= %
set $line=%$line:)=^^)%

for %%a in (%$line%) do echo %%a | find /i "oracle" || set $newpath=!$newpath!;%%a
set $newpath=!$newpath:#= !
set $newpath=!$newpath:^^=!
set path=!$newpath:~1!

And it is now working.

它现在正在工作。

回答by Vopel

I found the other solutions to this problem a bit awkward, I don't really want to rely on exact paths, complex 'delayed expansion' syntax, removing spaces for the 'for /f' loop and then adding them back in...

我发现这个问题的其他解决方案有点尴尬,我真的不想依赖精确路径、复杂的“延迟扩展”语法、删除“for / f”循环的空格然后将它们重新添加到......

I think this is more elegant, and I commented the hell out of it so even someone new to the horrors of Batch can follow along.

我认为这更优雅,我对此进行了评论,因此即使是对 Batch 的恐怖不熟悉的人也可以跟上。

::Turn off command display and allows environmental variables to be overridden for the current session
@echo off & setlocal

::Creates a unique file to use for the 'for loop'
set "TMPFILE="%temp%\tmp%RANDOM%%RANDOM%.txt""

::Duplicate PATH into OLDPATH
set "OLDPATH=%PATH%"

::Declare label for the 'goto' command
:Loop

::Extract the first text token with the default delimiter of semicolon
for /f "tokens=1 delims=;" %%G in ("%OLDPATH%") do (

REM Copy text token to TMPFILE unless what we want to remove is found
<NUL set /p="%%G" | find /i "StRiNgThAtMaTcHeSwHaTtOrEmOvE" >NUL 2>&1 || <NUL set /p="%%G;" >>%TMPFILE%

REM Remove text token from OLDPATH
set "OLDPATH=%OLDPATH:*;=%"
)

::Repeat loop until OLDPATH no longer has any delimiters, and then add any remaining value to TMPFILE
echo %OLDPATH% | findstr /C:";" >NUL && (goto :Loop) || <NUL set /p="%OLDPATH%" >>%TMPFILE%

::Set the path to TMPFILE
for /f "usebackq delims=" %%G in (%TMPFILE%) do (set "PATH=%%G")

::Clean-up
del %TMPFILE% >NUL 2>&1

::An echo and pause just for debug purposes
echo %PATH%
pause