Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch Script to delete oldest folder in a given folder

Tags:

batch-file

cmd

I'm writing a simple .bat backup script, and as part of it I want the oldest backup (folder) to be deleted upon reaching a set max limit of backups.

Right now I have this:

%COUNTER% is based on the number of backup folders currently in the directory where backups are stored, and is calculated earlier in the script.

%MAXBACKUPS% is just a user-specified number like "10," to say that you only want to keep up to 10 versions of the backups.

:: Delete the oldest backup, but only if we've exceeded the desired number of backups.
IF %COUNTER% gtr %MAXBACKUPS% (
ECHO More than %MAXBACKUPS% backups exist. Deleting oldest...
FOR /f "delims=" %%a in ('dir "..\Backup\*" /t:c /a:d /o:-d /b') do rd /s /q "..\Backup\%%a"
::Increment the counter down since we've just removed a backup folder.
SET /a COUNTER=%COUNTER%-1
)

I would like this script to only delete the one oldest folder in the ..\Backup folder, but as it stands it seems to delete every single folder it finds once it reaches the backup limit, which is obviously not the desired behavior.

like image 347
Ectropy Avatar asked Feb 11 '15 23:02

Ectropy


1 Answers

You were so close ! :-)

All you need to do is skip the first %MAXBACKUPS% entries when sorted by date descending. You don't even need your COUNTER variable.

:: Preserve only the %MAXBACKUPS% most recent backups.
set "delMsg="
for /f "skip=%MAXBACKUPS% delims=" %%a in (
  'dir "..\Backup\*" /t:c /a:d /o:-d /b'
) do (
  if not defined delMsg (
    set delMsg=1
    echo More than %MAXBACKUPS% found - only the %MAXBACKUPS% most recent folders will be preserved.
  )
  rd /s /q "..\Backup\%%a"
)
like image 111
dbenham Avatar answered Nov 25 '22 18:11

dbenham