Solved Delete backups based on name and date

August 29, 2012 at 02:33:05
Specs: Windows XP
Hello,

I create a daily backup of 3 database and a monthly backup of 2 in the same folder. The name structure is like this (filename +date)

The daily ones are named like this
Brokers120829
Brokers120828
Brokers120827

Monthly ones:
Intermediarios120731
Intermediarios12.06.30

Since i need to free up space in the workstation, i want a process to only leave the most recent 2 backups of all the databases and delete the older ones? is that possible


See More: Delete backups based on name and date

Report •


#1
August 29, 2012 at 03:26:56
✔ Best Answer
:: ===== script starts here ===============
::
:: keeplast.bat 2012-08-29 17:15:22.32
@echo off & setLocal enableDELAYedeXpansioN

for /f "tokens=* skip=1 delims= " %%a in ('dir/b/o-d Brokers*') do (
echo del %%a
)
for /f "tokens=* skip=1 delims= " %%a in ('dir/b/o-d Intermediarios*') do (
echo del %%a
)
::====== script ends here =================


====================================
Life is too important to be taken seriously.

M2


Report •

#2
August 29, 2012 at 03:39:54
Thank you very much! I'm really new at this, and i have another question.
How do i set up the path of the folder, will this only leave 2 of each? i want the .bat to run automatically everynight. Furthermore, does the filetype matter?

Thank you for your help


Report •

#3
August 29, 2012 at 04:44:24
This will not work if there is less than 3 retention backups.

@echo off & setlocal enabledelayedexpansion
set #=0
pushd c:\your\path
for /f "tokens=*" %%c in ('dir /b Brokers*') do set /a #+=1
set /a #-=#-2
for /f "skip=%#% tokens=* delims= " %%i in (
'dir /b/o-d Brokers*'
) do (
echo del %%i
)
set #=0
for /f "tokens=*" %%c in ('dir /b Intermediarios*') do set /a #+=1
set /a #-=#-2
for /f "skip=%#% tokens=* delims= " %%i in (
'dir /b/o-d Intermediarios*'
) do (
echo del %%i
)
popd

Tony


Report •

Related Solutions


Ask Question