One thing you need to be careful of with setting variables in batch is not to use spaces unnecessarily. (I don't see this in your question, but wanted to call it out since I caught myself doing it while troubleshooting your issue.) For example:
set x=123
is not the same as
set x = 123
The former will set %x%
to 123
, while the latter will set %x %
to 123
. (Note the space after x
in the variable name, and another one before 123
in the definition.)
Another thing to be aware of is you can't just say var=value
. To set an environment variable, you must use the SET
command.
So the proper syntax to set an environment variable that contains a path, and then one that expands upon that path, is as follows:
set x=C:\This\is\a\directory
set y=%x%\more\dirs
You can then use the SET
command to verify the contents of these variables.
If you're just learning scripting languages in Windows though, it would probably be more useful for you to skip batch and jump straight into PowerShell. PowerShell comes standard with Windows Vista and above, and is also available for XP. Here's how the same job would be done in PowerShell.
$x='C:\This\is\a\directory'
$y="$x\more dirs"
(Note: There's a reason double-quotes were used when setting $y
, while single-quotes were used for $x
. In this case, we could have used either for setting $x
but double-quotes were mandatory for setting $y
since we needed to expand a variable. You'll learn more about that as you pick up the language.)
Then, to verify the variables, you can just call them directly.
PowerShell is also a lot less sensitive to spacing in most cases.
$x=123
is the same as
$x = 123