3

The Windows command processor (cmd.exe) supports environment variables like USERNAME and USEPROFILE. The value of these variable is always the same after they have been set. But if I execute echo %TIME% I get a 'dynamic' value. So clearly there is an execution path for the environment variable syntax that can result in a dynamic value. (Unless the operating system actually updates the TIME variable every second. But I don't think so, because TIME is not listed when executing the set command.)

So how do I set an environment variable that results in execution of a script or program when evaluated?

Background:

I want to add information to my command prompt, based on the current working directory. The Windows Command Processor uses whatever is in the PROMPT environment variable as its prompt. If I can make an environment variable that is dynamic then I can probably achieve what I want.

  • Because nothing but cmd.exe provides %TIME%, it is most likely a special implementation. It is unlikely you would be able to replicate this without modifying cmd.exe. – Daniel B Jul 6 '17 at 11:29
  • Setting an environment variable based on your CWD doesn't make sense, you can have cmd.exe open multiple times each with different working directories. The environment variable must be the same across all of them. – JeffUK Jul 6 '17 at 11:55
  • @JeffUK This is why I'm searching for a way to have a dynamic environment variable, so that it can take the current working directory into account when it gets evaluated. %CD% is a relevant built-in example of a dynamic environment variable. – Michiel van Oosterhout Jul 6 '17 at 12:01
  • @DanielB Other dynamic environment variables can be used from other processes, like __APPDIR__. I would not be surprised if Windows can't be extended to support user-defined dynamic environment variables, but without asking here I would be less sure of that. Thanks – Michiel van Oosterhout Jul 6 '17 at 12:49
  • Dynamic environment variables such as TIME are very special that appear to exist only within command interpreters such as cmd.exe. If accessed from some other program they return nothing. I believe you can define dynamic environment variables for use within Powershell but not cmd.exe. – LMiller7 Jul 6 '17 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.