通过批处理从 %path% 变量中删除不需要的路径名 [英] Remove unwanted path name from %path% variable via batch

查看:64
本文介绍了通过批处理从 %path% 变量中删除不需要的路径名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Scope: Windows XP or newer Tools: Batch script

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

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% = C:;C:Program FilesCommon FilesJava;C:oracleproduct10.2.0in;C:WINDOWS;C:WINDOWSsystem32;

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

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:oracleproduct10.2.0in" 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...

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

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

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

推荐答案

你可以试试这样的:

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

我在最后一行放了一个 echo.检查结果,如果没问题,请将其删除.

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

这篇关于通过批处理从 %path% 变量中删除不需要的路径名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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