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

查看:144
本文介绍了是否有命令从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)

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

set oEnv = oShell.Environment(User )
为oEnv
oF中的每个sitem ile.WriteLine(SET& sitem)
next

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

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

  @echo off 
%〜dp0resetvars.vbs
调用%TEMP%\resetvars.bat

当您要刷新环境变量时,只需运行resetvars.bat。






apologetics



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



a。我找不到一个直接的方式将环境变量从vbs脚本导出到命令提示符,



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



我不知道一般规则是用户和系统之间的冲突变量,所以我选择让用户重写系统,除了专门处理的PATH变量中。



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

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



这可能会被改进。



ADDED



如果您需要从一个cmd窗口导出环境使用这个脚本(让我们称之为 exportvars.vbs ):

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

set oEnv = oShell.Environment(Process)
oEnv
oFile.WriteLine(SET&

oFile.Close

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

 %TEMP%\resetvars.bat
/ pre>

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:

@echo off
%~dp0resetvars.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天全站免登陆