1

In Windows 10, some environment variables actually use percentage signs to refer to paths. For example, the PSModulePath environment variable looks like this (ignore the additional one I marked out):

enter image description here

and you'll notice that it uses percentage signs to refer to certain directories instead of hard-coded paths. Suppose that I have several environment variables that I want to add, but I would like them to refer to custom directories of my own. For example, if I wanted to add some folder named "Include" located in some custom directory to an environment variable, ideally all I would have to do is add this text to the corresponding environment variable:

%MyCustomDirectoryVariable%\Include

But, of course, there's a problem; I'm not sure if it's even possible to create my own percentage sign variables that can refer to specific directories! Is it possible for me to create my own custom percentage sign variable that the environment variables can properly interpret as a directory? Or if I want to add my own path to an environment variable, must it always be a hard-coded path?

3
  • 1
    But those "percentage sign variables" are environment variables... Commented Jul 20, 2018 at 5:23
  • When working with the registry directly you have to take care to use the type REG_EXPAND_SZ instead of REG_SZ the GUI environment variable editor does this automatically. If you want to persistently (not just for the current session) set a variable in a batch use setx /?
    – LotPings
    Commented Jul 20, 2018 at 11:01
  • @LotPings, would you be able to give an example of how to use setx to achieve this? Everything I research just shows it being used to specifically modify the PATH environment variable, not how to create a new percentage sign one. Commented Jul 20, 2018 at 18:17

1 Answer 1

0

The easiest way is to use the set - command. Just open a commmand prompt and try:

set myvar = c:\sourcecode\include\

After that you can use %myvar% where you need it.

1
  • 1
    That's a very bad idea - you'll have an environment var myvar with a trailing space and the content has also a leading space. To use this variable you'd have to include the space %myvar %. Preferred syntax is `set "myvar=c:\sourcecode\include` but this answer totally misses OPs objective making a vartiable depending on another one.
    – LotPings
    Commented Jul 20, 2018 at 11:02

You must log in to answer this question.

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