Discussion:
delete files except the latest of every month
(too old to reply)
Ammammata
2021-02-10 11:35:28 UTC
Permalink
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
2021-02-10 13:28:20 UTC
Permalink
Post by Ammammata
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.
Ammammata
2021-02-15 10:18:49 UTC
Permalink
Il giorno Wed 10 Feb 2021 02:28:20p, *Kerr-Mudd,John* ha inviato su
Post by Kerr-Mudd,John
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
--
/-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
-=- -=- -=- -=- -=- -=- -=- -=- - -=-
........... [ al lavoro ] ...........
Herbert Kleebauer
2021-02-11 10:57:24 UTC
Permalink
Post by Ammammata
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
2021-02-11 11:03:03 UTC
Permalink
Post by Herbert Kleebauer
@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
2021-02-15 10:19:45 UTC
Permalink
Il giorno Thu 11 Feb 2021 12:03:03p, *Herbert Kleebauer* ha inviato su
Post by Herbert Kleebauer
Post by Herbert Kleebauer
@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.
thank you I'll give it a try
--
/-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
-=- -=- -=- -=- -=- -=- -=- -=- - -=-
........... [ al lavoro ] ...........
Ammammata
2021-02-15 14:22:54 UTC
Permalink
Il giorno Mon 15 Feb 2021 11:19:45a, *Ammammata* ha inviato su
Post by Ammammata
Post by Herbert Kleebauer
@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
--
/-\ /\/\ /\/\ /-\ /\/\ /\/\ /-\ T /-\
-=- -=- -=- -=- -=- -=- -=- -=- - -=-
........... [ al lavoro ] ...........
Herbert Kleebauer
2021-02-15 17:50:17 UTC
Permalink
Post by Ammammata
Post by Herbert Kleebauer
@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')
do echo del %%j) 2>nul
Fin Tres Nueve Dos
2021-05-03 05:38:29 UTC
Permalink
Have a look to the /D parameter of FORFILES command.

Loading...