title: How to keep the value of a variable outside a  Windows batch script which uses "delayed expansion local" mode?
body: <p><strong>Context</strong>: I need to call a Windows batch script which would update my <code>PATH</code> by adding another path '<code>xxx</code>' at the end of it, but:</p>
  
  <ul>
  <li>without any duplicate<br>
  (if I add '<code>xxx</code>' to a PATH like '<code>aaa;xxx;bbb</code>', I need an updated <code>PATH</code> like '<code>aaa;bbb;xxx</code>')</li>
  <li>without any aggregation<br>
  (I can call the script repeatedly without ending up with '<code>aaa;bbb;xxx;xxx;xxx;...</code>')</li>
  </ul>
  
  <p>What I have tried:</p>
  
  <p>The following function takes care of any duplicate and does the job</p>
  
  <pre><code>:cleanAddPath -- remove %~1 from PATH, add it at the end of PATH
  SETLOCAL ENABLEDELAYEDEXPANSION
  set PATH=!PATH:%~2=!
  set PATH=!PATH:;;=;!
  set PATH=%PATH%;%~2
  set P=!P:;;=;!
  echo %PATH%
  echo -------------
  ENDLOCAL  
  exit /b
  </code></pre>
  
  <p>But, it needs <a href="http://ss64.com/nt/delayedexpansion.html" rel="noreferrer"><strong>delayed expansion</strong> local mode</a>, which means: at the end of the script (or here, at the end of the function <code>cleanAddPath</code>), <strong>whatever has been set for <code>%PATH%</code> is thrown away</strong>.</p>
  
  <p>I could ask the users (for which I write the script) to launch their <code>cmd</code> with a <code>cmd /V:ON</code> option (activating the delayed expansion, otherwise off by default), but that is not practical.</p>
  
  <p>How can I modify the <code>PATH</code> variable the way I described above, and still have it updated in my current DOS session <em>after</em> calling said script?</p>