title: How can multiple dependent variables be set on the same line in a batch file?
body: <p>This was inspired by <a href="https://gaming.stackexchange.com/a/324287/53249">another post on Arqade</a>, on how to backup some files using a batch script.</p>
  
  <p>The following block creates a variable with the current date/time string:</p>
  
  <pre><code>REM -- Get the current date/time stamp
  set DS=%date%
  set TS=%time: =0%
  set mm=%DS:~4,2%
  set dd=%DS:~7,2%
  set yyyy=%DS:~10,4%
  set hh=%TS:~0,2%
  set min=%TS:~3,2%
  set ss=%TS:~6,2%
  set ms=%TS:~9,2%
  set DT_STAMP=%yyyy%-%mm%-%dd%_%hh%.%min%.%ss%
  </code></pre>
  
  <p>As a script writer, it is often convenient to consolidate these down to a single line. However, in this case, condensing this to a single line is extremely difficult, if not impossible.</p>
  
  <p>There does not seem to be a way to put multiple <code>set</code> commands on a single line. Separating commands with <code>&amp;</code> or <code>&amp;&amp;</code> works fine, but commands on the right cannot be dependent on variables that were set earlier on the same line.</p>
  
  <p>Also, notice how the <code>time</code> variable <strong>must</strong> have <code>spaces</code> replaced with zeros <code>0</code>. There does not seem to be a way to do both a string replacement <strong>and</strong> get a sub-string on the same line.</p>
  
  <p>Is there any way to get this down to one line? The closest I can get is two lines:</p>
  
  <pre><code>set DS=%date% &amp;&amp; set TS=%time: =0%
  set DT_STAMP=%DS:~10,4%-%DS:~4,2%-%DS:~7,2%_%TS:~0,2%.%TS:~3,2%.%TS:~6,2%
  </code></pre>
  
  <hr>
  
  <h3>Update</h3>
  
  <p>This has gathered some good answers; allow me to clarify what an accepted answer must have:</p>
  
  <ul>
  <li><strong>An ugly, but re-usable, single line solution is OK</strong> <em>(the pretty version is already provided above)</em></li>
  <li><strong>Standard shell commands only</strong> <em>(PowerShell and similar must be avoided; if I wanted those, I'd just do the whole thing in PowerShell)</em></li>
  <li>Ability to format the date in any logical format <em>(should be able to do anything the pretty version above can do, not just support ISO formatted dates using shorthand notation)</em></li>
  <li>It is OK to ignore localization settings <em>(really, just this one time!)</em></li>
  <li><strong>Must be on a single line!</strong> <em>(for instance, <a href="https://ss64.com/nt/delayedexpansion.html" rel="nofollow noreferrer">delayed expansion</a> is handy, but can have nasty side-effects, is not re-usable in every context, and usually requires multiple lines)</em></li>
  </ul>