I have see the older posts where you can delete files older than X days, such as this one Batch file to delete files older than N days. I'd like to add an additional filter to this so that if the backup I am running is not happening for 2 weeks, then it wouldn't delete all my backups.
I know with this:
forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del @path"
That you can delete older than X days but how do I add the condition above and have it leave a minimum of Z files behind before it does the delete?
Example:
Lets say I want to delete files older than 14 days but keep a minimum of 5 files with the files below:
Jan 1 - backup1.zip
Jan 2 - backup2.zip
Jan 3 - backup3.zip
Jan 4 - backup4.zip
Jan 5 - backup5.zip
Jan 6 - backup6.zip
Jan 7-20 no backups done
Jan 21st - script runs and removed old files
With the example code that deletes all files older than 14 days, all of my backup files would be deleted and I'd be left with no backups. I'd like the script to see that only 6 files remains and keep a minimum of 5 files.
Make sense? Is this possible through windows batch files?
FOR CLARITY
This is part of my batch file that I have:
cd /d %BKUPDIR%
FOR /f "skip=14 delims=" %%A IN ('DIR /a:-d /b /o:-d /t:c %1*.zip ^2^>nul') DO IF EXIST "%%~fA" ECHO "%%~fA" >>%LOGFILE%
FOR /f "skip=14 delims=" %%A IN ('DIR /a:-d /b /o:-d /t:c %1*.zip ^2^>nul') DO IF EXIST "%%~fA" DEL "%%~fA" >>%LOGFILE%
FOR /f "skip=14 delims=" %%A IN ('DIR /a:-d /b /o:-d /t:c %1*.log ^2^>nul') DO IF EXIST "%%~fA" ECHO "%%~fA" >>%LOGFILE%
FOR /f "skip=14 delims=" %%A IN ('DIR /a:-d /b /o:-d /t:c %1*.log ^2^>nul') DO IF EXIST "%%~fA" DEL "%%~fA" >>%LOGFILE%
The files are zip and log files that I am deleting and they all reside in the same folder.