是否有从 Windows 的命令提示符刷新环境变量的命令? [英] Is there a command to refresh environment variables from the command prompt in Windows?

查看:28
本文介绍了是否有从 Windows 的命令提示符刷新环境变量的命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我修改或添加环境变量,我必须重新启动命令提示符.有没有我可以在不重新启动 CMD 的情况下执行的命令?

If I modify or add an environment variable I have to restart the command prompt. Is there a command I could execute that would do this without restarting CMD?

推荐答案

可以用vbs脚本捕获系统环境变量,但是需要bat脚本才能真正改变当前的环境变量,所以这是一个组合解决方案.

You can capture the system environment variables with a vbs script, but you need a bat script to actually change the current environment variables, so this is a combined solution.

创建一个包含此代码的名为resetvars.vbs 的文件,并将其保存在路径中:

Create a file named resetvars.vbs containing this code, and save it on the path:

Set oShell = WScript.CreateObject("WScript.Shell")
filename = oShell.ExpandEnvironmentStrings("%TEMP%
esetvars.bat")
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set oFile = objFileSystem.CreateTextFile(filename, TRUE)

set oEnv=oShell.Environment("System")
for each sitem in oEnv 
    oFile.WriteLine("SET " & sitem)
next
path = oEnv("PATH")

set oEnv=oShell.Environment("User")
for each sitem in oEnv 
    oFile.WriteLine("SET " & sitem)
next

path = path & ";" & oEnv("PATH")
oFile.WriteLine("SET PATH=" & path)
oFile.Close

创建另一个包含此代码的文件名 resetvars.bat,位置相同:

create another file name resetvars.bat containing this code, same location:

@echo off
%~dp0resetvars.vbs
call "%TEMP%
esetvars.bat"

当你想刷新环境变量时,运行resetvars.bat

When you want to refresh the environment variables, just run resetvars.bat

道歉:

我提出这个解决方案的两个主要问题是

The two main problems I had coming up with this solution were

a.我找不到将环境变量从 vbs 脚本导出回命令提示符的直接方法,并且

a. I couldn't find a straightforward way to export environment variables from a vbs script back to the command prompt, and

b. PATH 环境变量是用户和系统 PATH 变量的串联.

b. the PATH environment variable is a concatenation of the user and the system PATH variables.

我不确定用户和系统之间的变量冲突的一般规则是什么,所以我选择让用户覆盖系统,除了专门处理的 PATH 变量.

I'm not sure what the general rule is for conflicting variables between user and system, so I elected to make user override system, except in the PATH variable which is handled specifically.

我使用怪异的vbs+bat+临时bat机制来解决从vbs导出变量的问题.

I use the weird vbs+bat+temporary bat mechanism to work around the problem of exporting variables from vbs.

注意:此脚本不会删除变量.

Note: this script does not delete variables.

这也许可以改进.

添加

如果您需要将环境从一个 cmd 窗口导出到另一个,请使用此脚本(我们称之为 exportvars.vbs):

If you need to export the environment from one cmd window to another, use this script (let's call it exportvars.vbs):

Set oShell = WScript.CreateObject("WScript.Shell")
filename = oShell.ExpandEnvironmentStrings("%TEMP%
esetvars.bat")
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set oFile = objFileSystem.CreateTextFile(filename, TRUE)

set oEnv=oShell.Environment("Process")
for each sitem in oEnv 
    oFile.WriteLine("SET " & sitem)
next
oFile.Close

在要导出的窗口中运行exportvars.vbs,然后切换到要导出的窗口,然后键入:

Run exportvars.vbs in the window you want to export from, then switch to the window you want to export to, and type:

"%TEMP%
esetvars.bat"

这篇关于是否有从 Windows 的命令提示符刷新环境变量的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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