windows 更改以从环境变量 PATH 中删除路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3072423/
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
Changing to remove path from environment variable PATH
提问by Bruce227
I'm trying to use a command line implementation to change the PATH
environment variable to remove a path, so I don't have to manually remove it on a bunch of machines.
我正在尝试使用命令行实现来更改PATH
环境变量以删除路径,因此我不必在一堆机器上手动删除它。
I have found this, which I can't seem to get it to work:
我发现了这个,我似乎无法让它工作:
%Path:str1=str2%
str1
is the path and str2
is null, which I'm not sure how to set it to null on the command line.
str1
是路径并且str2
为空,我不确定如何在命令行上将其设置为空。
If there is another way, I would be glad to give it a try.
如果有另一种方式,我很乐意尝试一下。
采纳答案by Bryan Ash
Using VBScript, you can get the path variable:
使用 VBScript,您可以获得路径变量:
dim shell, env, path, path_entries
set shell = createobject("wscript.shell")
set env = shell.environment("system")
path = env("path")
Then split to get an array of the pieces:
然后拆分以获取碎片数组:
path_entries = split(path, ";")
Set any entries to an empty string to remove them:
将任何条目设置为空字符串以删除它们:
path_entries(3) = ""
Then reconstruct the path:
然后重建路径:
path = join(path_entries, ";") ' elements in path are delimited by ";"
env("path") = path
回答by Grant Peters
I have found this, which I can't seem to get it to work: %Path:str1=str2% str1 is the path and str2 is null, which I'm not sure how to set it to null on the command line.
我发现了这个,我似乎无法让它工作: %Path:str1=str2% str1 是路径,str2 为空,我不确定如何在命令行上将其设置为空。
Not sure why this didn't work for you, but here is an example that does work (at least on Windows XP).
不知道为什么这对你不起作用,但这里有一个有效的例子(至少在 Windows XP 上)。
set path=%path:c:\windows\system32;=%
This will remove "c:\windows\system32;" from the path variable. Make sure you have the ;
on the end otherwise it may partially remove some other paths.
这将删除“c:\windows\system32;” 从路径变量。确保;
最后有 ,否则它可能会部分删除其他一些路径。
Remember that this will only affect the current instance of the command prompt. If you quit or work in a different command prompt, any changes you made to the environment variables will be lost.
请记住,这只会影响命令提示符的当前实例。如果您退出或在不同的命令提示符下工作,您对环境变量所做的任何更改都将丢失。
回答by Pavel Radzivilovsky
There's a difference between changing Path variable for a current process and/or for child processes, to changing the default load state of the variable when windows starts.
更改当前进程和/或子进程的 Path 变量与在 Windows 启动时更改变量的默认加载状态之间存在差异。
You might probably be able to do it with WMI. If not, take procmon and see what "My Computer" is doing when you edit a system variable. This will enable you to write a script.
您可能可以使用 WMI 来完成此操作。如果没有,请使用 procmon 并查看编辑系统变量时“我的电脑”在做什么。这将使您能够编写脚本。
回答by Ken Richards
In a vbScript command file (.cmd) or (.bat), you can use the following to remove an environment variable:
在 vbScript 命令文件 (.cmd) 或 (.bat) 中,您可以使用以下命令删除环境变量:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Environment("Process").Remove("PATH")
回答by 2ninup
There is an easier way instead of using the command prompt. Right click on "My Computer" go to advanced system settings, at bottom click Environment variables, highlight "PAth" and click "Edit". You can add, delete or change the order of directories in your path there.
有一种更简单的方法可以代替使用命令提示符。右键单击“我的电脑”进入高级系统设置,在底部单击环境变量,突出显示“路径”并单击“编辑”。您可以在那里添加、删除或更改路径中的目录顺序。
Hope this helps somebody, 2
希望这对某人有帮助,2