更改以从环境变量 PATH 中删除路径

发布于 2024-09-05 21:44:54 字数 268 浏览 5 评论 0原文

我正在尝试使用命令行实现来更改 PATH 环境变量以删除路径,因此我不必在一堆计算机上手动删除它。

我发现了这个,我似乎无法让它工作:

%Path:str1=str2%

str1 是路径,str2 是 null,我不知道如何设置它在命令行上设置为 null。

如果有其他方法,我很乐意尝试。

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.

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.

If there is another way, I would be glad to give it a try.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

离去的眼神 2024-09-12 21:44:54

我发现了这个,我似乎无法让它工作:%Path:str1=str2% str1 是路径,str2 为 null,我不知道如何将其设置为 null命令行。

不确定为什么这对您不起作用,但这里有一个有效的示例(至少在 Windows XP 上)。

set path=%path:c:\windows\system32;=%

这将删除“c:\windows\system32;”来自路径变量。确保末尾有 ; ,否则可能会部分删除其他一些路径。

请记住,这只会影响命令提示符的当前实例。如果退出或在不同的命令提示符下工作,对环境变量所做的任何更改都将丢失。

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.

Not sure why this didn't work for you, but here is an example that does work (at least on 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.

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.

时光瘦了 2024-09-12 21:44:54

使用 VBScript,您可以获得路径变量:

dim shell, env, path, path_entries
set shell = createobject("wscript.shell")
set env = shell.environment("system")
path = env("path")

然后拆分以获取片段数组:

path_entries = split(path, ";")

将任何条目设置为空字符串以删除它们:

path_entries(3) = ""

然后重建路径:

path = join(path_entries, ";") ' elements in path are delimited by ";"
env("path") = path

Using VBScript, you can get the path variable:

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
眼眸印温柔 2024-09-12 21:44:54

更改当前进程和/或子进程的 Path 变量与更改 Windows 启动时变量的默认加载状态之间存在差异。

您或许可以使用 WMI 来完成此操作。如果没有,请使用 procmon 看看编辑系统变量时“我的电脑”正在做什么。这将使您能够编写脚本。

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.

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.

香草可樂 2024-09-12 21:44:54

在 vbScript 命令文件 (.cmd) 或 (.bat) 中,您可以使用以下命令删除环境变量:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Environment("Process").Remove("PATH")

In a vbScript command file (.cmd) or (.bat), you can use the following to remove an environment variable:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Environment("Process").Remove("PATH")
盗琴音 2024-09-12 21:44:54

有一种更简单的方法可以代替使用命令提示符。右键单击“我的电脑”进入高级系统设置,在底部单击环境变量,突出显示“路径”,然后单击“编辑”。您可以在其中添加、删除或更改路径中的目录顺序。

希望这对某人有帮助,
2

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文