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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
giulia.1996
Posts: 2
Joined: 19 Feb 2020 04:47

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

#1 Post by giulia.1996 » 19 Feb 2020 05:01

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

scavenger
Posts: 18
Joined: 23 May 2015 13:51
Contact:

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

#2 Post by scavenger » 22 Apr 2020 14:19

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
)

Post Reply