Page 1 of 1

Clear content of a dir based on the creation date of files

Posted: 19 Feb 2020 05:01
by giulia.1996
Hello!

I have a server where I put backups, that consist in a huge amount of heavy files...the backup are scheduled weekly (but sometimes the backup can have problems and not be executed) and the free disk space is always getting smaller...I have done a batch for copy the files on a QNAP (for security purpose) but now I need to give to the server some free space...

My idea is to make a batch file that erase all the files older that the last backup...I know that is simpler to say to the server "erase all the files older that X days" but if the scheduled backup have an error and don't write files on my server I have the risk to have a emply folder!

How can i say to the server to:
1) find the newest file based on the date of creation (only the date, so not the time)
2) erase all the oldest file oldest that the date identificated in the point 1

It can be ideal to identify the 2 newest date of creations to save from the erase the last two backups but if not possible is not a big problem...

If not possible to make a batch accoring to my idea, it is possible to say to the server to:
1) sort the file from the newer to the older (I can do it)
2) keep in the directory the first 40 files of teh dir and erase the others

Thank you!!!

Giulia

Re: Clear content of a dir based on the creation date of files

Posted: 22 Apr 2020 14:19
by scavenger
easy way: sort files by creation date reversed and delete all but the first one:

Code: Select all

pushd %folder%
for /f %A in ('dir /b /o-d /TC %filePattern%') do (
IF DEFINED lastOne echo del %A
set lastOne=true
)