0

I have a bunch of batch files which I want to run sequentially. One of them runs an MSI which adds a folder to the PATH. How can I make sure that subsequent batch files will notice this change, without restarting CMD? I'm using Windows Server 2008 R2 x64.

I've tried call, cmd /c and start "", in the hope that starting a new process will work, but it doesn't.

in run-both-scripts.bat
call script1.bat  <-- This runs an MSI which modifies the PATH
call script2.bat  <-- This relies on the PATH changes which were made by the MSI in script1.bat

To clarify: this is fairly straightforward to reproduce.

  1. Start CMD
  2. Create an environment variable manually, not using setx, to mimic what the MSI does.
    1. Right click on Computer -> Properties -> Advanced System Settings -> Environment variables -> New
    2. Create an environment variable called, say, hello with the value hi there.
  3. In your CMD window, type echo %hello%. You'll get %hello%.
  4. Try cmd /c "echo %hello%. You'll get %hello%.
  5. Try start "" to open a new CMD process; type echo %hello%. You'll get %hello%.
  6. Try start "" echo %hello% to run the command in a new CMD process. You'll get %hello%.
  7. Finally, try manually opening a new CMD window from the Start menu and type echo %hello% from there. You'll see hi there.

So you can see that the only way I've been able to make CMD see the change to the environment variable is by restarting CMD.

4

1 回答 1

0

好的,做了一些研究,并找出了为什么我们一直在向您抛出的解决方案不起作用。当您将 cmd.exe 作为应用程序启动时,它会查看当前环境变量并将其复制到内存中。当您在批处理文件中启动 cmd 时,它不会查看环境变量,而是查看当前批处理文件中设置的变量,并利用这些变量。这就是您将数据存储在内存上时的问题。唯一可行的方法是将当前环境变量复制到一个文本文件中,作为硬盘上的内存。现在,问题是如何去做。

经过大量研究,我发现与该主题相关的唯一一件事就是使用start /i,但是,当我对此进行测试时,它不起作用。(start /?了解更多信息)。

换句话说,其他的setx,我不认为这对批处理是可能的。

莫娜

于 2013-08-13T22:58:40.387 回答