3

I wish to define two environment variables as follows:

PROGRAM_HOME=P:
JAVA_DIR=%PROGRAM_HOME%\Java

Obviously the following works:

dir %PROGRAM_HOME%

But, the following doesn't:

dir %JAVA_DIR%

What should I do in the case I do wish to define JAVA_DIR in terms of PROGRAM_HOME?

EDIT: removed \ (it had been a typo)

PROGRAM_HOME=P:\ ==>PROGRAM_HOME=P:

3
  • What exactly "doesn't work"? Do you get an error message?
    – fretje
    Jan 4, 2010 at 14:55
  • "The system cannot find the file specified." This is because, if I set the variable from the GUI dialog the %PROGRAM_HOME% variable does not get expanded. I use Rapid Environment Editor, by the way.
    – Yaneeve
    Jan 4, 2010 at 15:30
  • Then your problem is apparently that Rapid Environment Editor doesn't support expanding other environment variables.
    – fretje
    Jan 4, 2010 at 18:03

3 Answers 3

1

Apart from the syntax error found by fretje, if you wish to make these variables permanent, then you need to change the Windows environment variables.

This is done in Control Panel / System / Advanced tab / Environment variables... button.

There is also a nice little product called Rapid Environment Editor (donationware) that provides a clearer user interface.

EDIT

I think I understand now what you're trying to do, but that can only be done by editing the registry. The following Microsoft article explains the process to follow:
How to propagate environment variables to the system.

In a nutshell, for user environment variables you need to go to registry key HKEY_CURRENT_USER\Environment, where all these variables are stored.

There you need to add the two variables as of type REG_EXPAND_SZ (named "expanded string value" in regedit). This is the only way to add this type, since Windows by default will only add REG_SZ types which are simply unexpandable.

I tried it just now and it works perfectly well.

2
  • I do use that Editor, but it can only 'expand' env vars embedded within other env vars to a depth of 1, hope I made myself clear
    – Yaneeve
    Jan 4, 2010 at 14:51
  • Got it! See my edit.
    – harrymc
    Jan 4, 2010 at 16:49
4

I think it's because you repeat the '\'.

When I do the following:

>set %program_home%=c:\
>set %java_dir%=%program_home%java
>dir %java_dir%

everything works fine.

Of course an alternative is:

>set %program_home%=c:
>set %java_dir%=%program_home%\java
>dir %java_dir%

You can verify the values of the different environment variables by simply issuing an set command without parameters. Then you'll see that your %java_dir% variable contains c:\\java.

2
  • ok, that works, but I wish to set the vars, once and for all, not via the cmd. What would you suggest?
    – Yaneeve
    Jan 4, 2010 at 13:40
  • 1
    Yaneeve, I think you may be mis-reading his answer. Remove the \ in "JAVA_DIR=%PROGRAM_HOME%\Java" in your environment variable and it should work. Note that the syntax fretje uses here with set can be used identically with system environment variables.
    – RJFalconer
    Jan 4, 2010 at 14:07
2

Yes this a really odd behavior that I have never found documented but here is what works.

Using your example above put quotes around the variable in the last line:

dir "%JAVA_DIR%"

If someone knows the why of this or a link that explains it this would be great. The behavior is consistent though when referring to a variable that has been defined in part with another variable.

1
  • For some reason this works only if I use the 'set' command to define the variable within the same process and not otherwise :|
    – Yaneeve
    Jan 4, 2010 at 11:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .