如何制作更改环境变量的Windows批处理文件?

时间:2021-09-08 11:44:39

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

是否可以创建一个可以对环境变量进行持久更改的批处理文件?

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.

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

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 

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

不幸的是,这不起作用,因为install.bat永远不会“返回”主脚本。永远不会调用myapplication.exe。接下来我尝试了:

cmd /C install.bat
myapplication.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.

不幸的是,这不起作用,因为这意味着install.bat在一个完全独立的cmd.exe shell中运行。这意味着一旦脚本终止,环境变量更改都不会持续,因为cmd.exe也会终止。

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

必须有一种方法来创建一个更改环境变量的批处理文件

Any suggestions?

3 个解决方案

#1


In your case, what you want is

在你的情况下,你想要的是什么

rem install_and_run.bat
call install.bat 
myapplication.exe

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

也就是说,使用call来调用install.bat,这样控件就会返回install_and_run.bat。

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.

我想你不明白环境变量是按进程的。您的批处理文件正在cmd.exe的实例中运行,并且该实例具有环境。编写cmd / C时,您正在创建cmd.exe的新实例,该实例具有自己的环境。然后install.bat正在对cmd.exe的新实例的环境进行“持久”更改。

#2


well, you could do:

好吧,你可以这样做:

Set EnvVariableName="Some Value"

设置EnvVariableName =“一些值”

as to your calling a seperate batch file from within another batch file, I believe the call command will return

至于你从另一个批处理文件中调用一个单独的批处理文件,我相信call命令将返回

rem install_and_run.bat
call install.bat
myapplication.exe

rem install_and_run.bat调用install.bat myapplication.exe

#3


You can't create or modify an environment variable and have it persist between console sessions from a batch script, AFAIK.

您无法创建或修改环境变量,并使其在批处理脚本AFAIK的控制台会话之间保持不变。

I have a VBScript script with a batch script wrapper that I use for this, but I think Jay has the correct solution for you. If you like though, I can post my code.

我有一个带有批处理脚本包装的VBScript脚本,我用它,但我认为Jay有适合你的解决方案。如果您愿意,我可以发布我的代码。

#1


In your case, what you want is

在你的情况下,你想要的是什么

rem install_and_run.bat
call install.bat 
myapplication.exe

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

也就是说,使用call来调用install.bat,这样控件就会返回install_and_run.bat。

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.

我想你不明白环境变量是按进程的。您的批处理文件正在cmd.exe的实例中运行,并且该实例具有环境。编写cmd / C时,您正在创建cmd.exe的新实例,该实例具有自己的环境。然后install.bat正在对cmd.exe的新实例的环境进行“持久”更改。

#2


well, you could do:

好吧,你可以这样做:

Set EnvVariableName="Some Value"

设置EnvVariableName =“一些值”

as to your calling a seperate batch file from within another batch file, I believe the call command will return

至于你从另一个批处理文件中调用一个单独的批处理文件,我相信call命令将返回

rem install_and_run.bat
call install.bat
myapplication.exe

rem install_and_run.bat调用install.bat myapplication.exe

#3


You can't create or modify an environment variable and have it persist between console sessions from a batch script, AFAIK.

您无法创建或修改环境变量,并使其在批处理脚本AFAIK的控制台会话之间保持不变。

I have a VBScript script with a batch script wrapper that I use for this, but I think Jay has the correct solution for you. If you like though, I can post my code.

我有一个带有批处理脚本包装的VBScript脚本,我用它,但我认为Jay有适合你的解决方案。如果您愿意,我可以发布我的代码。