Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 Files\Common Files\Java;C:\oracle\product\10.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...

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.

like image 558
user3208239 Avatar asked Jan 22 '14 17:01

user3208239


People also ask

How do I remove a PATH from $path?

We can comment or remove the last line from the /etc/profile. d/jdk. sh script to remove Java's path from the $PATH variable permanently. After removing the path from the file, when a user starts a new session, it won't have that directory in the $PATH variable.

How do you separate paths in environment variables?

In the Environment Variables window (pictured below), highlight the Path variable in the System variables section and click the Edit button. Add or modify the path lines with the paths you want the computer to access. Each directory path is separated with a semicolon, as shown below.


1 Answers

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

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 setx instead of set and open another console for changes to take effect.

setx /M PATH "%PATH:C:\Program Files (x86)\Git\bin;=%" 
like image 83
Jens A. Koch Avatar answered Oct 11 '22 18:10

Jens A. Koch