Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

delete files except the latest of every month

17 views
Skip to first unread message

Ammammata

unread,
Feb 10, 2021, 6:35:31 AM2/10/21
to
there's a backup scheduled daily for many different files

the related filenames are unique, i.e.

RSP_backup_2021_02_09_200005_8336305.bak
RSP_backup_2021_02_08_200007_8776502.bak
RSP_backup_2021_02_07_200005_8652391.bak
RSP_backup_2021_02_06_200005_4658428.bak
RSP_backup_2021_02_05_200005_1944601.bak
RSP_backup_2021_02_04_200005_6798320.bak
RSP_backup_2021_02_03_200005_4282408.bak
RSP_backup_2021_02_02_200005_0172624.bak
RSP_backup_2021_02_01_200005_9353583.bak
RSP_backup_2021_01_31_200006_2371866.bak
RSP_backup_2020_12_31_200003_2429004.bak
RSP_backup_2020_10_24_200004_0066296.bak

I'd like to delete, about every 3-4 months, all the backups except the
lates of every single month, tipically those with date 31/10/20 30/11/20
31/12/20 31/01/21 etc

in the above list you can see I kept current month (feb) and left the
latest of each previous one (jan, dec, oct)

I do this manually, right now it's not a huge tast, using a file manager,
but I wonder whether there is an automated solution since the different
backups will grow

TIA

--
/-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
-=- -=- -=- -=- -=- -=- -=- -=- - -=-
........... [ al lavoro ] ...........

Kerr-Mudd,John

unread,
Feb 10, 2021, 8:28:22 AM2/10/21
to
On Wed, 10 Feb 2021 11:35:28 GMT, Ammammata <amma...@tiscalinet.it>
wrote:

> there's a backup scheduled daily for many different files
>
> the related filenames are unique, i.e.
>
> RSP_backup_2021_02_09_200005_8336305.bak
> RSP_backup_2021_02_08_200007_8776502.bak
> RSP_backup_2021_02_07_200005_8652391.bak
> RSP_backup_2021_02_06_200005_4658428.bak
> RSP_backup_2021_02_05_200005_1944601.bak
> RSP_backup_2021_02_04_200005_6798320.bak
> RSP_backup_2021_02_03_200005_4282408.bak
> RSP_backup_2021_02_02_200005_0172624.bak
> RSP_backup_2021_02_01_200005_9353583.bak
> RSP_backup_2021_01_31_200006_2371866.bak
> RSP_backup_2020_12_31_200003_2429004.bak
> RSP_backup_2020_10_24_200004_0066296.bak
>
> I'd like to delete, about every 3-4 months, all the backups except the
> lates of every single month, tipically those with date 31/10/20
> 30/11/20 31/12/20 31/01/21 etc
>
> in the above list you can see I kept current month (feb) and left the
> latest of each previous one (jan, dec, oct)
>
> I do this manually, right now it's not a huge tast, using a file
> manager, but I wonder whether there is an automated solution since the
> different backups will grow
>
> TIA
>

For /F /?

It would be simpler to retain the *1st* backup of each month.



--
Bah, and indeed, Humbug.

Herbert Kleebauer

unread,
Feb 11, 2021, 5:57:28 AM2/11/21
to
On 10.02.2021 12:35, Ammammata wrote:
> there's a backup scheduled daily for many different files
>
> the related filenames are unique, i.e.
>
> RSP_backup_2021_02_09_200005_8336305.bak
> RSP_backup_2021_02_08_200007_8776502.bak
> RSP_backup_2021_02_07_200005_8652391.bak
> RSP_backup_2021_02_06_200005_4658428.bak
> RSP_backup_2021_02_05_200005_1944601.bak
> RSP_backup_2021_02_04_200005_6798320.bak
> RSP_backup_2021_02_03_200005_4282408.bak
> RSP_backup_2021_02_02_200005_0172624.bak
> RSP_backup_2021_02_01_200005_9353583.bak
> RSP_backup_2021_01_31_200006_2371866.bak
> RSP_backup_2020_12_31_200003_2429004.bak
> RSP_backup_2020_10_24_200004_0066296.bak
>
> I'd like to delete, about every 3-4 months, all the backups except the
> lates of every single month, tipically those with date 31/10/20 30/11/20
> 31/12/20 31/01/21 etc
>
> in the above list you can see I kept current month (feb) and left the
> latest of each previous one (jan, dec, oct)
>
> I do this manually, right now it's not a huge tast, using a file manager,
> but I wonder whether there is an automated solution since the different
> backups will grow

If the names are exactly as given above, this should work (remove 'echo'
before 'del' if the output is ok to actual delete the files):

@echo off
for %%i in (01 02 03 04 05 06 07 08 09 10 11 12) do (
for /f "skip=1" %%j in ('dir /b o-n RSP_backup_202?_%%i_??_??????_???????.bak') do echo del %%j) 2>nul


Herbert Kleebauer

unread,
Feb 11, 2021, 6:03:05 AM2/11/21
to
On 11.02.2021 11:57, Herbert Kleebauer wrote:
> @echo off
> for %%i in (01 02 03 04 05 06 07 08 09 10 11 12) do (
> for /f "skip=1" %%j in ('dir /b o-n RSP_backup_202?_%%i_??_??????_???????.bak') do echo del %%j) 2>nul

Sorry, this only works if there are not more backups than
for 12 month in the directory. Otherwise you have to loop
also through the years.


Ammammata

unread,
Feb 15, 2021, 5:18:55 AM2/15/21
to
Il giorno Wed 10 Feb 2021 02:28:20p, *Kerr-Mudd,John* ha inviato su
alt.msdos.batch il messaggio
news:XnsACCD890C51...@144.76.35.252. Vediamo cosa ha scritto:

>
> It would be simpler to retain the *1st* backup of each month.
>
>

yes, actually the first or the last is near the same, I just want to keep
one for each month

Ammammata

unread,
Feb 15, 2021, 5:19:48 AM2/15/21
to
Il giorno Thu 11 Feb 2021 12:03:03p, *Herbert Kleebauer* ha inviato su
alt.msdos.batch il messaggio news:s032t7$7ut$1...@gioia.aioe.org. Vediamo
cosa ha scritto:
thank you I'll give it a try

Ammammata

unread,
Feb 15, 2021, 9:22:58 AM2/15/21
to
Il giorno Mon 15 Feb 2021 11:19:45a, *Ammammata* ha inviato su
alt.msdos.batch il messaggio
news:XnsACD27341EB20am...@127.0.0.1. Vediamo cosa ha
scritto:

>> @echo off
>> for %%i in (01 02 03 04 05 06 07 08 09 10 11 12) do (
>> for /f "skip=1" %%j in ('dir /b o-n
>> RSP_backup_202?_%%i_??_??????_???????.bak') do echo del %%j) 2>nul


>
> thank you I'll give it a try
>
>

well, once removed the 'safety' echo, it does some deletions leaving just
one file per month even if in an apparent random mode, I think there is a
missing slash in dir /b o-n, should be dir /b /o-n

let me try again, I worked on a copy ;)

so, with the change it leaves just the first of each month, it's anyway a
valid solution, thank you

Herbert Kleebauer

unread,
Feb 15, 2021, 12:50:20 PM2/15/21
to
On 15.02.2021 15:22, Ammammata wrote:

>>> @echo off
>>> for %%i in (01 02 03 04 05 06 07 08 09 10 11 12) do (
>>> for /f "skip=1" %%j in ('dir /b o-n
>>> RSP_backup_202?_%%i_??_??????_???????.bak') do echo del %%j) 2>nul

> well, once removed the 'safety' echo, it does some deletions leaving just
> one file per month even if in an apparent random mode, I think there is a
> missing slash in dir /b o-n, should be dir /b /o-n
>
> let me try again, I worked on a copy ;)
>
> so, with the change it leaves just the first of each month, it's anyway a
> valid solution, thank you

Yes, the / was missing, but /o-n should leave the last of the month
whereas /on the first of the month.

If there are backups for more than a year, just specify the year which you
want to clean up:

@echo off
for %%i in (01 02 03 04 05 06 07 08 09 10 11 12) do (
for /f "skip=1" %%j in ('dir /b /o-n RSP_backup_2021_%%i_??_??????_???????.bak')

Fin Tres Nueve Dos

unread,
May 3, 2021, 1:38:30 AM5/3/21
to
Have a look to the /D parameter of FORFILES command.
0 new messages