Aacini wrote:This is my point of view: %CD% variable (current directory) is NOT stored into the environment, but created by cmd.exe when %CD% is used, in the same way as %DATE%, %TIME%, %RANDOM% and other dynamic variables. However, SETLOCAL/ENDLOCAL commands make %CD% variable behave like if it was stored in the environment, like any other regular variable! This is a very particular mechanism that, in my opinion, requires that SETLOCAL command store the current directory in a memory area separated from the environment, but paralell to it (that is, to each instance of the environment), and that ENDLOCAL command restore current directory from such area.
I am in total agreement. Your description is compatible with my description of dynamic variables (type 1) found at http://stackoverflow.com/a/20169219/1012053
. I also agree that the saved current directory info must be distinct from the saved environment info.
I have never seen any documentation describing the current directory behavior. It makes me wonder if there are any other ??? that are saved and restored by SETLOCAL/ENDLOCAL. But I haven't found anything other than true environment variables and the current directory (including current drive).
I've tested the following (on Win 7), but none are restored by ENDLOCAL:
MODE (I've only tested window and buffer sizes)
For a brief moment I thought that perhaps PROMPT was special, but then I realized that it is normal environment variable restoration at play. CMD.EXE uses the value of the PROMPT variable to determine what gets displayed when ECHO is ON.
The PROMPT command simply sets the PROMPT environment variable - it is a functional synonym for SET PROMPT=
PROMPT without any arguments simply undefines the PROMPT variable.
The trick is that CMD.EXE defaults to $P$G if the PROMPT variable is not defined. The other oddity is that during intialization, CMD.EXE automatically defines PROMPT=$P$G if PROMPT is not already defined.