Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete all but X most recent folders

Can someone provide a batch script that will delete all but the X most recently modified folders in a directory. I've looked at How do I delete old files from a directory while keeping the most recent ones on Windows, but that was based on an absolute time window rather than a relative ordering of modification dates.

Thanks for any help

like image 358
rimsky Avatar asked Nov 22 '25 04:11

rimsky


1 Answers

This will keep the 10 latest log files based on modification date:

@echo off
for /f "skip=10 delims=" %%a in (' dir *.log /o-d /a-d /b ') do echo del "%%a"

Remove the echo to make it perform the deletions rather than just display them.

like image 138
foxidrive Avatar answered Nov 23 '25 21:11

foxidrive



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!