Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

windows - Remove unwanted path name from %path% variable via batch

Scope: Windows XP or newer Tools: Batch script

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;

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...

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

set PATH=%PATH:C:Program Files (x86)Gitin;=%

You might use this to see the change:

echo %PATH:C:Program Files (x86)Gitin;=% | tr ; 

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

If you need to make it a persistent change use setx instead of set and open another console for changes to take effect.

setx /M PATH "%PATH:C:Program Files (x86)Gitin;=%"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...