title: Get date for N days before
body: <p>I found some problem with my bat script which is dedicated to find date in format YYYYMMDD, but still can't figure out reason of such behavior.
  <br><br>Script code:</p>
  
  <pre><code>@ECHO OFF
  SetLocal EnableDelayedExpansion
  SetLocal
  
  SET CmdFile="C:\cmd.ini"
  SET StartDate = ""
  
  call:getDate %1 StartDate
  
  IF EXIST %CmdFile% del %CmdFile%
  
  (
      echo RangeBegin=20%StartDate%
  ) &gt; %CmdFile%
  
  :getDate
  set day=%~1
  echo &gt;"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
  echo&gt;&gt;"%temp%\%~n0.vbs" WScript.Echo year(s)^&amp; right(100+month(s),2)^&amp; right(100+day(s),2)
  for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
  set "YYYY=%result:~0,4%"
  set "MM=%result:~4,2%"
  set "DD=%result:~6,2%"
  set "data=%YYYY:~2,4%%mm%%dd%"
  set "%~2=%data%"
  goto:eof
  
  endlocal
  </code></pre>
  
  <p>So, I call a script like <code>makecmd.bat -10</code> and expect to find a new file in <code>C:\</code> drive named <code>cmd.ini</code> with following context:</p>
  
  <pre><code>RangeBegin=20170627
  </code></pre>
  
  <p>But got this:</p>
  
  <pre><code>RangeBegin=20,4~4,2~6,2
  </code></pre>
  
  <p>What am I doing wrong?</p>