title: How to keep only the five newest backup folders and delete the older?
body: <p>I want to use a batch file to make a backup regularly, every day. Consider these folders: </p>
  
  <pre><code>C:\game folder
  D:\backup1
  D:\backup2
  D:\backup3
  D:\backup4
  D:\backup5
  </code></pre>
  
  <p>There are inside game folder:</p>
  
  <pre><code>c:\game folder\remote\                       »Folder
  c:\game folder\remote\remotecache.vdf        ».vdf file
  c:\game folder\remote\FullProfile            »No extension file
  c:\game folder\remote\ProfileSaves\          »Folder
  c:\game folder\remote\ProfileSaves\1054678   »No extension file
  </code></pre>
  
  <p>I need a batch file that</p>
  
  <ul>
  <li>verifies if time stamp on <code>backup1</code> (either folder or any file inside it)
  is older than 1 day and if YES then continue, else stop;</li>
  <li>deletes <code>backup5</code> folder or its contents; copies folders/files of <code>backup4</code> to <code>backup5</code> folder; </li>
  <li>deletes <code>backup4</code> folder or its contents; copies folders/files of <code>backup3</code> to <code>backup4</code> folder; </li>
  <li>etc. up to <code>backup1</code> folder. </li>
  <li>Finally copies game folder to <code>backup1</code> folder.</li>
  </ul>
  
  <p>Is it possible?</p>
  
  <p>My idea is to run a scheduled task (Windows), and the .bat file verifies if backup is needed or not by using time stamp of <code>backup1</code> or any file inside it, comparing with actual date and checking if <code>backup1</code> is older than one day.</p>
  
  <hr>
  
  <p>Considering the code below, what should I place in order to make it works as I've described before?</p>
  
  <pre><code>@echo off
  
  echo  
  echo ------------------------------------------------------------------
  echo Daily script that backs-up game folder
  echo ------------------------------------------------------------------
  
  echo ------------------------------------------------------------------
  echo Calculation of date
  for /f "delims=" %%a in ('wmic OS Get localdatetime  ^| find "."') do set "dt=%%a"
  set "YY=%dt:~2,2%"
  set "YYYY=%dt:~0,4%"
  set "MM=%dt:~4,2%"
  set "DD=%dt:~6,2%"
  set "HH=%dt:~8,2%"
  set "Min=%dt:~10,2%"
  set "Sec=%dt:~12,2%"
  
  set datestamp=%YYYY%%MM%%DD%
  set timestamp=%HH%%Min%%Sec%
  set fullstamp=%YYYY%-%MM%-%DD%_%HH%-%Min%-%Sec%
  </code></pre>
  
  <p>How to make some kind of <strong>IF</strong> here to check time stamp of <code>backup1</code> folder and if it's older than 1 day run the code below? And how to set the <code>@path</code>?</p>
  
  <pre><code>echo ------------------------------------------------------------------
  echo Delete old backup folder
  FORFILES -p "" /D -1 /C "cmd /c IF @isdir == TRUE rd /S /Q @path"
  
  echo ------------------------------------------------------------------
  echo Make new Backup folder
  md D:\Backup1 
  
  echo ------------------------------------------------------------------
  echo Copy files into backup folder
  xcopy /s /y "C:\game folder" "D:\Backup1"
  </code></pre>