Note - this question is based on behavior observed on Windows 7. I believe the behavior applies to all other versions from Vista onward. Based on MC ND\'s answer, and Fo
[This should be a comment, but it would be a very large one!]
I would like to add that %CD% variable is also a special case for the following reason: if a setlocal
command is executed and then the current directory is changed, a posterior endlocal
command change the current directory back to the one active when setlocal
was executed:
@echo off
echo Original: %CD%
setlocal
md newdir
cd newdir
echo In newdir: %CD%
endlocal
echo After endlocal: %CD%
This behavior indicate that %CD% dynamic variable is saved with setlocal
's and restored with (explicit or implicit) endlocal
's, in the same way of normal variables. However, a weird point is that this mechanism works even if a user CD variable is defined before or after the setlocal
! The conclusion is that setlocal
command saves the current directory in an area separated from the environment variables, and that endlocal
command restore the current directory from that area.