我怎样才能使Windows批处理文件,改变一个环境变量? [英] How can I make a Windows batch-file which changes an environment variable?

查看:90
本文介绍了我怎样才能使Windows批处理文件,改变一个环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使一个批处理文件,它可以使一个永久以一个环境变量的变化?

Is it possible to make a batch file which can make a persistent change to an environment variable?

例如我installer.bat脚本复制一些文件在计算机的文件系统中的随机位置即可。我想那个位置添加到PATH环境变量,以便程序可以在当前会话中运行。

For example my installer.bat script copies some files to a random location in the computer's file-system. I'd like to add that location to the PATH environment variable so that the programs can be run in the current session.

仅供参考 - 我非常频繁变动安装的东东:我想做一个全新安装每一次我运行该程序。另外,我不希望以防万一其他一些(旧的)实例仍执行的过度写程序的其他previously安装副本。

FYI - the stuff I am installing changes very frequently: I want to do a fresh install every single time I run the program. Also, I do not want to over-write other previously installed copies of the program just in case some other (older) instance is still executing.

我希望能够做这样的事:

I'd like to be able to do something like this:

rem install_and_run.bat
install.bat 
myapplication.exe

可惜,这是不行的,因为从来没有INSTALL.BAT收益主脚本。 myapplication.exe不会被调用。接下来,我尝试:

Unfortunately this does not work, because the install.bat never "returns" to the main-script. myapplication.exe is never called. Next I tried:

cmd /C install.bat
myapplication.exe

不幸的是,这并不工作,因为它意味着INSTALL.BAT是在一个完全独立的cmd.exe shell中运行。这意味着,没有环境变量的变化坚持一旦脚本终止,因为CMD.EXE也将终止。

Unfortunately this does not work because it means that install.bat is run in an entirely seperate cmd.exe shell. That means none of the environment variable changes persist once the script terminates because the cmd.exe also terminates.

必须有一种方法,使一个批处理文件,它改变了环境变量

There must be a way to make a batch-file which changes environment variables

有什么建议?

推荐答案

在你的情况,你要的是

rem install_and_run.bat
call install.bat 
myapplication.exe

也就是说,使用呼叫调用 INSTALL.BAT ,以便控制将返回到 install_and_run.bat

That is, use call to call install.bat, so that control will return to install_and_run.bat.

我想你不明白,环境变量的每个进程即可。您的批处理文件中的cmd.exe的一个实例运行,该实例有一个环境。当你写 CMD / C 您已创建的cmd.exe的新实例,它有它自己的环境。然后 INSTALL.BAT 正在对CMD.EXE的新实例的环境中的老大难的变化。

I think you don't understand that environment variables are per-process. Your batch file is running in an instance of cmd.exe, and that instance has an environment. When you wrote cmd /C you were creating a new instance of cmd.exe, which has its own environment. Then install.bat was making a "persistent" change to the environment of the new instance of cmd.exe.

这篇关于我怎样才能使Windows批处理文件,改变一个环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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