title: When is the CD environment variable updated?
body: <p>The tipp to use the <code>CD</code> environment variable from batch scripts to get the current working directory is commonly posted.  But <code>CD</code> won't get updated when <code>call</code>ing another batch file.  Then the <code>cd</code> command echoes the new path of the other batch file, but <code>%CD%</code> (or <code>!CD!</code>) is not updated. Example:</p>
  
  <pre><code>@echo off
   cd %~dp0
   echo in %0: CD=%CD%
   pause
   call X:\testcall.cmd
  </code></pre>
  
  <p>Save this as <code>C:\testcall.cmd</code> and <code>X:\testcall.cmd</code>, then run <code>C:\testcall.cmd</code>. You should see that the value of <code>CD</code> has not changed. This seems not to dependend on <code>call</code>; none of the following works:</p>
  
  <pre><code>start /D &lt;NEW_DIR&gt; &lt;OTHER_CMD_FILE&gt;
  start cmd /c &lt;NEW_DIR&gt;\&lt;OTHER_CMD_FILE&gt;
  cmd /c &lt;NEW_DIR&gt;\&lt;OTHER_CMD_FILE&gt;
  &lt;NEW_DIR&gt;\&lt;OTHER_CMD_FILE&gt;
  cd %~dp0
  pushd %~dp0
  </code></pre>
  
  <p><code>CD</code> will keep it's old value, while <code>cd</code> (the command) shows the correct directory. Therefore I set <code>CD</code> at the begin of a script:</p>
  
  <pre><code>set CD=%~dp0
  </code></pre>
  
  <p>...while assuming <em>cmd.exe</em> sets <code>CD</code> only if this variable is yet unset. True?</p>