1

Assuming I have the following set as system environment variables in Windows 7

FOO = foo
path = ...;%FOO%/bin

Take the following example run in cmd

set FOO=bar
start cmd
echo %FOO%
//bar
echo %path%
//...;foo/bin

The path environment variable did not re evaluate itself upon launch of cmd, however the new FOO variable did stick. How can I get path to reevaulate itself based on the new FOO variable set in the parent command terminal?

EDIT: I'm looking for path to become ...;bar/bin

kwolfe
  • 1,607
  • 2
  • 12
  • 27
  • Path is just a string `"...;foo/bin"`, it doesn't remember that it originally came from variable `%FOO%` interpolation. So there's nothing to re-evaluate. – Ben Voigt Oct 14 '14 at 15:13
  • It reevaluates each time cmd.exe is opened from a shortcut. (eg, change FOO in system settings and relaunch cmd.exe from shortcut or run prompt) – kwolfe Oct 14 '14 at 15:34
  • What would be the command line equivalent of opening cmd.exe from the desktop or start menu? – kwolfe Oct 14 '14 at 15:37
  • What is your purpose/goal? (What's provoking the question?) – Bill_Stewart Oct 14 '14 at 16:05
  • What do you imagine is the relationship between the new command interpreter instance spawned by `start cmd` and the running batch file? It's one-way, the new instance will inherit everything from the current instance, the current instance will be unaffected by the new instance. – Ben Voigt Oct 14 '14 at 16:24
  • @Dipto: Why did you take out the desired result? – Ben Voigt Oct 14 '14 at 16:24
  • What is the purpose/goal? (i.e., what's provoking the question?) – Bill_Stewart Oct 15 '14 at 19:13
  • @Ben: Sorry. That was a mistake I just wanted to do some spelling correction and tag addition. I did not mean to delete the desired result. – DotPi Oct 19 '14 at 23:20

2 Answers2

1

There is no "command line equivalent of opening cmd.exe from the desktop or start menu" that reproduces the behavior you care about. Environment variables are inherited from the parent process.

The shell reads the registry and performs interpolation (using its own environment, which is the process of being read from the registry, and knows nothing of variables you set in a command interpreter), which is why updates to the registry settings are reflected in a cmd.exe launched from the shell.

If you launch a new cmd.exe from a running cmd.exe, you won't get the shell behavior, and you will get the existing environment inherited. There's nothing in Windows that uses variables in a command interpreter to interpolate the registry settings. The code responsible for reading the environment from the registry is completely unrelated to cmd.exe... it is in explorer.exe (or probably one of the shell DLLs used by explorer).

This answer, which uses VB Script to read the registry and construct a batch file, is as good as you can get. I haven't tested whether interpolation is performed in the registry-access COM component (Environment("System") method on a WScript.Shell object) used by VB script, or if the environment variable references survive into the batch file and are interpolated during batch processing. So you may be confounded by the order of evaluations and variable assignments, in which case you'd better adapt the script to fetch just the PATH setting itself and leave all other variables alone.

Community
  • 1
  • 1
Ben Voigt
  • 260,885
  • 36
  • 380
  • 671
  • Thank you very much for taking the time to answer. I will give this a try and accept your answer if it works. Very well detailed answer as to why I'm not getting the desired results, though! – kwolfe Oct 14 '14 at 19:02
0

I also found this to be useful

set path=%path:foo/bin=bar/bin%

Not as dynamic as I wanted but it works to replace a portion of a variable.

kwolfe
  • 1,607
  • 2
  • 12
  • 27