0

I am currently backing up a folder on my desktop to another folder every 6 hours with Task Scheduler and a batch file using xcopy. How do I, say, delete backups older than 12 backups ago? For example if I have 12 backups already and it's the next 6-hour mark, it backs up the source folder and then deletes the oldest backup, as now there's 13 so that there's only 12. And this repeats, so it cycles backups.

My folders are formatted like so: YYYY-MM-DD_HHMM

(24-hour format)

Shane Smiskol
  • 952
  • 1
  • 11
  • 38

1 Answers1

1

You were not very clear. Assuming, your backups are folders, named something like BackupYYYYMMDD

dir /b /ad /o-n backup* gives you your backupfolders (simple format, directories only, sorted by name (reversed to start with the newest) *)

Put that into a for /f to parse the output by skipping 12 lines, resulting in:

for /f "skip=12 tokens=*" %%i in ('dir /b /ad /o-n backup*') do ECHO rd /s /q %%i

Remove the ECHO, when the output seems ok.

*) if you have another dateformat, better use /o-d (sorted by date, newest first)

for backupfolders named like YYYY-MM-DD_HHMM you can use dir /b /ad /o-n ????-??-??_???? to minimize the chances that any other folders are counted or affected.

Stephan
  • 53,940
  • 10
  • 58
  • 91