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

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

问题描述

如果我修改或添加环境变量,我必须重新启动命令提示符(轻微的不便)。有没有一个命令,我可以执行,这样做而无需重新启动CMD?

解决方案

您可以捕获系统环境变量与vbs脚本,但您需要一个bat脚本来实际更改当前环境变量,因此这是一个组合的解决方案。



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

 设置oShell = WScript.CreateObject WScript.Shell)
filename = oShell.ExpandEnvironmentStrings(%TEMP%\resetvars.bat)
设置objFileSystem = CreateObject(Scripting.fileSystemObject)
设置oFile = objFileSystem .createTextFile(filename,TRUE)

设置oEnv = oShell.Environment(System)
为oEnv中的每个站点
oFile.WriteLine(SET& sitem)
next
path = oEnv(PATH)

设置oEnv = oShell.Environment(User)
为oEnv中的每个站点
oFile .WriteLine(SET& sitem)
next

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

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

  resetvars.vbs 
call%TEMP%\resetvars.bat

环境变量,只需运行resetvars.bat。







$ b

这个解决方案遇到的两个主要问题是



a。一个直接的方式将环境变量从vbs脚本导出回命令提示符,并且



b。 PATH环境变量是一个连接用户和系统PATH变量。



我不知道用户和系统之间的冲突变量的一般规则是什么,所以我选择使用户覆盖系统,除非在具体处理的PATH变量中。



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



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



/ p>

ADDED



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

 设置oShell = WScript.CreateObject(WScript.Shell)
filename = oShell.ExpandEnvironmentStrings(%TEMP%\resetvars.bat)
设置objFileSystem = CreateObject(Scripting.fileSystemObject)
设置oFile = objFileSystem.CreateTextFile(filename,TRUE)

设置oEnv = oShell.Environment(Process)
为oEnv中的每个站点
oFile.WriteLine SET& sitem)
next
oFile.Close

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

 %TEMP%\resetvars.bat


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

解决方案

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.

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%\resetvars.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

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

resetvars.vbs
call "%TEMP%\resetvars.bat"

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


Apologetics:

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

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

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

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.

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.

This can probably be improved.

ADDED

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%\resetvars.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

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

"%TEMP%\resetvars.bat"

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

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