Why can't I access a variable named __CD__ on Windows 7?

前端 未结 5 2171
渐次进展
渐次进展 2020-12-01 14:47

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

5条回答
  •  执笔经年
    2020-12-01 14:54

    [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.

提交回复
热议问题