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.

5
  • 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
    Commented Jul 6, 2017 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
    Commented Jul 6, 2017 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. Commented Jul 6, 2017 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 Commented Jul 6, 2017 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
    Commented Jul 6, 2017 at 16:29

0

You must log in to answer this question.

Browse other questions tagged .